UNPKG

@tanstack/hotkeys

Version:

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

114 lines (112 loc) • 4.64 kB
import { isModifierCode, modifierAlias } from "./_chord.js"; import { ownValue } from "./_keys.js"; import { PUNCTUATION_CODE_MAP, normalizeKeyName } from "./constants.js"; // src/_keyboard-event.ts /** * The letter printed on the key for a macOS Option press, from the legacy * `keyCode`, or undefined when `keyCode` isn't a letter (A to Z). */ function macKeycapLetter(event) { return event.keyCode > 64 && event.keyCode < 91 ? String.fromCharCode(event.keyCode) : void 0; } /** 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, modifier presses, and events without a key, * such as the ones autofill sends, are not steps: they neither advance a * sequence nor reset it. */ function isSequenceStep(event) { return !!event.key && !(event.type === "keydown" && event.repeat) && !isComposingEvent(event) && !isModifierPress(event); } /** * Whether a key event that matches no step leaves a sequence's progress alone: * a lock key, Fn, or a dead key, whose character comes with the next key. */ function keepsSequenceProgress(event) { return /^(CapsLock|NumLock|ScrollLock|Fn|FnLock|Dead)$/.test(event.key); } /** * 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); const punctuation = ownValue(PUNCTUATION_CODE_MAP, code); if (punctuation !== void 0) return punctuation; return ownValue({ NumpadAdd: "+", NumpadSubtract: "-", NumpadMultiply: "*", NumpadDivide: "/", NumpadDecimal: ".", NumpadEnter: "Enter", Space: "Space" }, code); } /** * Whether a key value is text the key typed rather than a named key such as * `End` or `Dead`. Text can take several code points: macOS Hebrew Shift+A * types `שׁ`, a letter with a point. */ function isTypedText(key) { return key !== "" && !/^[A-Z][A-Za-z0-9]+$/.test(key); } /** * 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 isTypedText(event.key) && !/^[A-Za-z0-9]$/.test(event.key); } export { altGraphTypedCharacter, codeToLogicalKey, isModifierPress, isSequenceStep, isTypedText, keepsSequenceProgress, macKeycapLetter, normalizeKeyboardEvent };