@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
80 lines (78 loc) • 4.44 kB
JavaScript
import { altGraphTypedCharacter, codeToLogicalKey, normalizeKeyboardEvent } from "./_keyboard-event.js";
import { isAsciiLetter, isLatinLetter, isPrintableKey, keysEqual } from "./_keys.js";
import { isSingleLetterKey } from "./constants.js";
import { parseHotkey } from "./parse.js";
import { detectPlatform } from "./platform.js";
// src/_match.ts
/** Matches an event and reports the winning candidate and release identity. */
function matchKeyboardEvent(event, hotkey, platform = detectPlatform()) {
const parsed = typeof hotkey === "string" ? parseHotkey(hotkey, platform) : hotkey;
const normalized = normalizeKeyboardEvent(event, platform);
const noMatch = {
matched: false,
score: 0
};
if (normalized.isComposing) return noMatch;
const { altGraph } = normalized;
if ((altGraph ? false : normalized.ctrl) !== parsed.ctrl || (altGraph ? false : normalized.alt) !== parsed.alt || normalized.meta !== parsed.meta) return noMatch;
const shiftMatches = normalized.shift === parsed.shift;
if (altGraph && !altGraphTypedCharacter(normalized)) return noMatch;
if (parsed.code !== void 0) return !altGraph && shiftMatches && normalized.code === parsed.code ? matched("code", 3) : noMatch;
if (keysEqual(normalized.key, parsed.key)) {
const shiftTypedNothing = platform === "mac" && normalized.meta && normalized.shift && !normalized.alt && !/^(Digit|Numpad)/.test(normalized.code) && codeToLogicalKey(normalized.code) === normalized.key;
const implicitShift = isPrintableKey(normalized.key) && !isSingleLetterKey(normalized.key) && !parsed.shift && !shiftTypedNothing;
if (shiftMatches || implicitShift && normalized.shift) return matched("key", implicitShift ? 2 : 3);
}
if (altGraph || !shiftMatches) return noMatch;
if (normalized.key === "ı" && parsed.key === "I" && !normalized.alt) return matched("key", 2);
const optionLetter = platform === "mac" && normalized.alt && (normalized.key === "Dead" || isPrintableKey(normalized.key) && !isAsciiLetter(normalized.key)) && event.keyCode > 64 && event.keyCode < 91 ? String.fromCharCode(event.keyCode) : void 0;
if (optionLetter === void 0 && !positionStandsIn(normalized, platform)) return noMatch;
const fallbackKey = optionLetter ?? codeToLogicalKey(normalized.code);
return fallbackKey !== void 0 && keysEqual(fallbackKey, parsed.key) ? matched("fallback", 1) : noMatch;
function matched(source, score) {
return {
matched: true,
score,
source,
identity: {
key: normalized.key,
code: normalized.code
}
};
}
}
/**
* Whether a key's conventional position may stand in for what it typed. Only
* output that can't name a shortcut falls back:
* - non-Latin letters and symbols, such as Russian `ы` or macOS Option's `–`
* - dead keys
* - `Unidentified` keys, except on letter positions, which layouts such as
* AZERTY rearrange, and numpad positions, whose output depends on NumLock.
* Chrome on Linux Wayland reports Ctrl+` this way, and Firefox reports
* numpad 5 with NumLock off this way.
* - anything on the digit row, where AZERTY types `&` and `é` for 1 and 2
* - ASCII symbols typed with Shift, such as `!` for Shift+1
* - ASCII symbols typed with macOS Option on a letter position, such as `~`
* for US Cmd+Option+N, where Command cancels the dead key
* - Latin letters typed with Alt, such as macOS Option+A typing `å`
* - Latin letters on punctuation keys with Control or Command, so German
* Cmd+ü still fires `Mod+[` (German types `[` with Option)
*
* A named key (`End` from a NumLock-off numpad), any other ASCII character,
* or a Latin letter typed on its own is the key the user meant, wherever it
* sits. AZERTY's `+` on the US `/` position is `+`, not `/`. So is a Latin
* letter typed with Control+Alt on a Windows letter key: Windows treats
* Control+Alt as AltGr, so Polish Control+Alt+A types `ą`.
*/
function positionStandsIn({ key, code, ctrl, meta, shift, alt }, platform) {
if (key === "Unidentified") return !/^(Key|Numpad)/.test(code);
if (key !== "Dead" && !isPrintableKey(key)) return false;
if (code.startsWith("Digit")) return true;
if (isAsciiLetter(key)) return false;
const letterKey = code.startsWith("Key");
if (/^[!-~]$/.test(key)) return shift || alt && platform === "mac" && letterKey;
if (!isLatinLetter(key)) return true;
if (alt && !(letterKey && ctrl && platform === "windows")) return true;
return (ctrl || meta) && !letterKey;
}
export { matchKeyboardEvent };