UNPKG

@tanstack/hotkeys

Version:

Type-safe, framework-agnostic keyboard hotkey management for the browser

91 lines (89 loc) • 3.71 kB
import { isModifierCode, modifierAlias } from "./_chord.js"; import { isPrintableKey } from "./_keys.js"; import { PUNCTUATION_CODE_MAP, normalizeKeyName } from "./constants.js"; // src/_keyboard-event.ts /** Normalizes Unicode key names and modifier flags while retaining physical code and IME state. */ function normalizeKeyboardEvent(event, platform) { let altGraph = false; try { altGraph = platform !== "mac" && typeof event.getModifierState === "function" && event.getModifierState("AltGraph"); } catch {} const key = (event.key || "").normalize("NFC"); const normalizedKey = normalizeKeyName(key); const controlAlt = platform === "windows" && altGraph && !altGraphTypedCharacter({ key: normalizedKey, altGraph }); return { key: normalizedKey, code: event.code || "", ctrl: controlAlt || Boolean(event.ctrlKey), shift: Boolean(event.shiftKey), alt: controlAlt || Boolean(event.altKey), meta: Boolean(event.metaKey), altGraph: altGraph && !controlAlt, location: event.location, isComposing: isComposingEvent(event) }; } /** * Whether a key press belongs to an input method editor, so no hotkey reads it. * * Safari sends the Enter that commits a composition after compositionend, * with isComposing false but keyCode 229. An `Unidentified` key counts only * without a usable code: Chrome on Linux Wayland sends Ctrl+Backquote as an * `Unidentified` key with its code. One on a modifier key, such as * `ControlLeft`, is ignored like a modifier press. */ function isComposingEvent(event) { return Boolean(event.isComposing) || event.keyCode === 229 || event.key === "Process" || event.key === "Unidentified" && (!event.code || event.code === "Unidentified" || isModifierCode(event.code)); } /** * Whether the event is the press or release of a modifier key itself: * Control, Shift, Alt, Meta, or AltGraph. The key can be missing on events * that autofill and browser extensions send. */ function isModifierPress(event) { return event.key === "AltGraph" || modifierAlias(event.key || "") !== void 0; } /** * Whether a key event can be a step of a sequence. Auto-repeats, keys an * input method editor reads, and modifier presses are not steps: they neither * advance a sequence nor reset it. */ function isSequenceStep(event) { return !(event.type === "keydown" && event.repeat) && !isComposingEvent(event) && !isModifierPress(event); } /** * Maps familiar positions to compatibility fallback keys for the logical matcher. * This assumes conventional positions, not the active layout; never use it to * turn recorded physical bindings into logical bindings or display labels. */ function codeToLogicalKey(code) { if (/^Key[A-Z]$/i.test(code)) return code.slice(3).toUpperCase(); if (/^Digit[0-9]$/i.test(code)) return code.slice(5); if (/^Numpad[0-9]$/i.test(code)) return code.slice(6); if (Object.hasOwn(PUNCTUATION_CODE_MAP, code)) return PUNCTUATION_CODE_MAP[code]; const named = { NumpadAdd: "+", NumpadSubtract: "-", NumpadMultiply: "*", NumpadDivide: "/", NumpadDecimal: ".", NumpadEnter: "Enter", Space: "Space" }; return Object.hasOwn(named, code) ? named[code] : void 0; } /** * Whether AltGr changed what a key typed, as in AltGr+Q typing `@`. False when * AltGr is not held, or when the key typed its plain value: a named key such * as Home, or an ASCII letter or digit (AltGr+F typing `f` on layouts with * nothing on that key). */ function altGraphTypedCharacter(event) { if (!event.altGraph) return false; if (event.key === "Dead") return true; return isPrintableKey(event.key) && !/^[A-Za-z0-9]$/.test(event.key); } export { altGraphTypedCharacter, codeToLogicalKey, isModifierPress, isSequenceStep, normalizeKeyboardEvent };