@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
86 lines (84 loc) • 4.38 kB
JavaScript
import { isCodeName, isKeyNameShape, isModifierCode } from "./_chord.js";
import { altGraphTypedCharacter, codeToLogicalKey, isModifierPress, normalizeKeyboardEvent } from "./_keyboard-event.js";
import { isAsciiLetter, isLatinLetter, isPrintableKey } from "./_keys.js";
import { matchKeyboardEvent } from "./_match.js";
import { normalizeHotkey, parseKeyboardEvent } from "./parse.js";
// src/_recorder-chord.ts
/**
* Converts a keydown to a chord that fires for the same key press.
*
* - `'auto'` records the key by name when the press has one, and its physical
* code otherwise.
* - `'code'` always records the physical code.
* - `'key'` always records the character or key name the press produced.
*
* Returns null for composition, modifier-only input, and a press the mode
* can't record: a modifier position or unusable physical input by code, or an
* `Unidentified` key by key.
*/
function hotkeyChordFromKeydown(event, platform, recordBy = "auto") {
const normalized = normalizeKeyboardEvent(event, platform);
if (normalized.isComposing || isModifierPress(event)) return null;
const parsed = parseKeyboardEvent(event, platform);
const { key: _key, code: _code, ...modifiers } = parsed;
if (recordBy === "auto") {
const key = keyName(event, platform);
if (key !== void 0 && matchKeyboardEvent(event, {
...modifiers,
key
}, platform).matched) return normalizeHotkey({
...modifiers,
key
}, platform);
}
if (recordBy === "code" || recordBy === "auto" && hasUsableCode(event)) return hasUsableCode(event) && !isModifierCode(event.code) && !normalized.altGraph ? normalizeHotkey({
...modifiers,
code: event.code
}, platform) : null;
return parsed.key && parsed.key !== "Unidentified" && (recordBy === "key" || parsed.key !== "Dead" && isKeyNameShape(parsed.key)) ? normalizeHotkey(parsed, platform) : null;
}
/**
* The name `'auto'` records a press by, or undefined when its physical code
* says more than anything it typed:
*
* - A named key such as Enter and a letter are recorded as typed.
* - Any other character is recorded as typed without Shift. With Shift, the
* position is recorded: macOS reports `/` for Cmd+Shift+/ where Windows
* reports `?`, and `1` for Cmd+Shift+1 where Windows reports `!`, so the
* typed character would not name the same press on both.
* - A non-Latin letter is named by its position, as the matcher names it:
* Russian `ы` is `S`.
* - macOS Option types another character or starts an accent, so the keycap
* letter is read from `keyCode`, as the matcher reads it. Option with any
* other key keeps its position.
* - Dead keys, whitespace characters, `Unidentified` keys, the numpad (what
* it types depends on NumLock), modifier positions and AltGr presses keep
* their position.
*/
function keyName(event, platform) {
const { key, alt, shift, altGraph } = normalizeKeyboardEvent(event, platform);
if (!key || altGraph || key === "Unidentified" || /^Numpad/.test(event.code) || isModifierCode(event.code)) return;
const typed = key !== "Dead" && isPrintableKey(key);
if (key !== "Dead" && !typed) return isKeyNameShape(key) ? key : void 0;
if (platform === "mac" && alt) return isAsciiLetter(key) ? key : event.keyCode > 64 && event.keyCode < 91 ? String.fromCharCode(event.keyCode) : void 0;
if (!typed || !key.trim()) return void 0;
if (isLatinLetter(key)) return key;
if (/^\p{Letter}$/u.test(key)) return codeToLogicalKey(event.code);
return shift ? void 0 : key;
}
/**
* Explains why a key press cannot be recorded before syntax/policy checks.
* Recording by key accepts produced characters instead of requiring a code,
* but rejects AltGr that typed nothing new, which no hotkey matches.
*/
function chordRejection(event, platform, recordBy = "auto") {
const normalized = normalizeKeyboardEvent(event, platform);
if (recordBy === "key") return normalized.altGraph && !altGraphTypedCharacter(normalized) ? { reason: "alt-graph" } : void 0;
if (normalized.altGraph) return { reason: "alt-graph" };
if (!hasUsableCode(event) && (recordBy === "code" || hotkeyChordFromKeydown(event, platform, recordBy) === null)) return { reason: "missing-code" };
}
/** Whether the event reports a real physical key code to record. */
function hasUsableCode(event) {
return isCodeName(event.code) && event.code !== "Unidentified";
}
export { chordRejection, hotkeyChordFromKeydown };