UNPKG

@tanstack/hotkeys

Version:

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

32 lines (30 loc) • 1.54 kB
import { resolveHotkeys } from "./_alternates.js"; import { MODIFIER_KEYS, normalizeKeyName } from "./constants.js"; import { isHotkeySequence, parseHotkey } from "./parse.js"; // src/hint.ts /** * Returns whether held modifiers should reveal a shortcut hint. * By default, any nonempty subset of the binding's modifiers qualifies; an * unrelated modifier hides the hint. `exact` requires every binding modifier. * Nonmodifier keys are ignored. This does not check registration or focus. * A sequence reveals its hint from its first step, and a hotkey object from * the hotkey its label shows: its entry for the platform, or its first * alternate where the platform has only alternates. Returns false for no * hotkey (`undefined`, * `null`, or `''`). * * @example * matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt']) // true * matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt', 'Control']) // false */ function matchesHeldModifiers(hotkey, heldKeys, options = {}) { const resolved = resolveHotkeys(hotkey, options.platform)[0]; if (!resolved) return false; if (heldKeys.includes("AltGraph")) return false; const held = [...new Set(heldKeys.map(normalizeKeyName).filter((key) => MODIFIER_KEYS.has(key)))]; const first = isHotkeySequence(resolved) ? resolved[0] : resolved; if (first === void 0) return false; const { modifiers } = parseHotkey(first, options.platform); return held.length > 0 && held.every((key) => modifiers.includes(key)) && (!options.exact || held.length === modifiers.length); } export { matchesHeldModifiers };