UNPKG

@tanstack/hotkeys

Version:

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

332 lines (330 loc) • 10.5 kB
import { createStore } from "./_store.js"; import { MODIFIER_KEYS, normalizeKeyName } from "./constants.js"; import { detectPlatform } from "./platform.js"; // src/key-state-tracker.ts const LOCK_KEYS = [ "CapsLock", "NumLock", "ScrollLock" ]; function unknownLocks() { return { CapsLock: null, NumLock: null, ScrollLock: null }; } /** * Returns the default state for KeyStateTracker. */ function getDefaultKeyStateTrackerState() { return { heldKeys: [], heldCodes: {}, locks: unknownLocks() }; } /** * Singleton tracker for currently held keyboard keys. * * This class maintains a list of all keys currently being pressed, * which is useful for: * - Displaying currently held keys to users * - Custom shortcut recording for rebinding * - Complex chord detection * * State Management: * - Uses TanStack Store for reactive state management * - State can be accessed via `tracker.store.state` when using the class directly * - When using framework adapters (React), use `useHeldKeys` and `useHeldKeyCodes` hooks for reactive state * * @example * ```ts * const tracker = KeyStateTracker.getInstance() * * // Access state directly * console.log(tracker.store.state.heldKeys) // ['Control', 'Shift'] * * // Subscribe to changes with TanStack Store * const unsubscribe = tracker.store.subscribe(() => { * console.log('Currently held:', tracker.store.state.heldKeys) * }) * * // Check current state * console.log(tracker.getHeldKeys()) // ['Control', 'Shift'] * console.log(tracker.isKeyHeld('Control')) // true * * // Cleanup * unsubscribe() * ``` */ var KeyStateTracker = class KeyStateTracker { static #instance = null; /** * The TanStack Store instance containing the tracker state. * Use this to subscribe to state changes or access current state. */ store = createStore(getDefaultKeyStateTrackerState()); #heldEntries = /* @__PURE__ */ new Map(); /** Read once, since mousemove reads lock state many times a second. */ #mac = detectPlatform() === "mac"; /** Lock key states, published with the next held key or lock change. */ #locks = unknownLocks(); /** Window listeners as `[type, listener, capture]`, removed together on destroy. */ #listeners = []; constructor() { this.#setupListeners(); } /** * Gets the singleton instance of KeyStateTracker. */ static getInstance() { if (!KeyStateTracker.#instance) KeyStateTracker.#instance = new KeyStateTracker(); return KeyStateTracker.#instance; } /** * Resets the singleton instance. Useful for testing. */ static resetInstance() { if (KeyStateTracker.#instance) { KeyStateTracker.#instance.destroy(); KeyStateTracker.#instance = null; } } /** * Sets up the keyboard event listeners. */ #setupListeners() { if (typeof window === "undefined" || typeof window.addEventListener !== "function") return; const onKeydown = (event) => { if (isKeyless(event)) return; const locksChanged = this.#readLocks(event); let changed = this.#reconcileModifiers(event); const key = normalizeKeyName(event.key); const identity = event.code || `key:${key}`; if ((MODIFIER_KEYS.has(key) || key === "AltGraph" || !event.isComposing && event.keyCode !== 229) && !this.#heldEntries.has(identity)) { this.#heldEntries.set(identity, { key, code: event.code }); changed = true; } this.#publish(changed, locksChanged); }; const onKeyup = (event) => { if (isKeyless(event)) return; const locksChanged = this.#readLocks(event); const size = this.#heldEntries.size; const key = normalizeKeyName(event.key); const identity = event.code || `key:${key}`; this.#heldEntries.delete(identity); if (!event.code) { for (const [entryIdentity, entry] of this.#heldEntries) if (entry.key === key) this.#heldEntries.delete(entryIdentity); } this.#reconcileModifiers(event); if (key === "Meta" && this.#mac) this.#releaseNonModifiers(); this.#publish(this.#heldEntries.size !== size, locksChanged); }; const onContextMenu = (event) => { if (event.defaultPrevented || !event.isTrusted) return; const reconciled = this.#reconcileModifiers(event); if (this.#releaseNonModifiers() || reconciled) this.#syncState(); }; const onMouse = (event) => { const locksChanged = this.#readLocks(event); this.#publish(this.#reconcileModifiers(event), locksChanged); }; const onBlur = () => { const locksChanged = this.#updateLocks(unknownLocks()); const held = this.#heldEntries.size > 0; this.#heldEntries.clear(); this.#publish(held, locksChanged); }; this.#listeners = [ [ "keydown", onKeydown, true ], [ "keyup", onKeyup, true ], [ "mousemove", onMouse, true ], [ "mousedown", onMouse, true ], [ "blur", onBlur, false ], [ "contextmenu", onContextMenu, false ], [ "visibilitychange", () => document.hidden && onBlur(), true ] ]; for (const [type, listener, capture] of this.#listeners) window.addEventListener(type, listener, capture); } /** * Removes modifiers whose release was missed without a window blur. * Event modifier state is shared by left/right keys, so a true flag keeps * both physical entries until their individual keyup events arrive. * Only real user input counts: events dispatched by page code report no * modifiers and would clear keys the user is still holding. */ #reconcileModifiers(event) { const size = this.#heldEntries.size; if (event.isTrusted) { for (const [identity, entry] of this.#heldEntries) if (entry.key === "Control" && !event.ctrlKey || entry.key === "Alt" && !event.altKey || entry.key === "Shift" && !event.shiftKey || entry.key === "Meta" && !event.metaKey || entry.key === "AltGraph" && typeof event.getModifierState === "function" && !event.getModifierState("AltGraph")) this.#heldEntries.delete(identity); } return this.#heldEntries.size !== size; } /** * Reads lock key states from real user input. Events dispatched by page * code report every lock as off. Mouse events still reach an unfocused * window, where a lock can change in another app without a new blur, so * those are skipped. macOS has no Num Lock or Scroll Lock: Chrome reports * them off, and Firefox reports Num Lock for numpad keys. */ #readLocks(event) { if (!event.isTrusted || !event.getModifierState || !document.hasFocus()) return false; const locks = unknownLocks(); for (const key of LOCK_KEYS) if (key === "CapsLock" || !this.#mac) locks[key] = event.getModifierState(key); return this.#updateLocks(locks); } /** Stores lock key states for the next publish. Returns whether any changed. */ #updateLocks(locks) { if (LOCK_KEYS.every((key) => locks[key] === this.#locks[key])) return false; this.#locks = locks; return true; } /** * Publishes one state per event. A held key change rebuilds the held key * state and carries the locks with it. A lock change alone keeps the held * key arrays, so held key selectors don't see a new value. */ #publish(heldChanged, locksChanged) { if (heldChanged) this.#syncState(); else if (locksChanged) this.store.setState((state) => ({ ...state, locks: this.#locks })); } /** Forgets every held key that isn't a modifier. Returns whether any was held. */ #releaseNonModifiers() { const size = this.#heldEntries.size; for (const [identity, entry] of this.#heldEntries) if (!MODIFIER_KEYS.has(entry.key)) this.#heldEntries.delete(identity); return this.#heldEntries.size !== size; } /** * Syncs the internal Set to the Store state. */ #syncState() { const heldKeys = Array.from(new Set(Array.from(this.#heldEntries.values(), (entry) => entry.key))); const heldCodes = Object.fromEntries(Array.from(this.#heldEntries.values(), (entry) => [entry.key, entry.code])); this.store.setState((state) => ({ ...state, heldKeys, heldCodes, locks: this.#locks })); } /** * Removes the keyboard event listeners. */ #removeListeners() { for (const [type, listener, capture] of this.#listeners) window.removeEventListener(type, listener, capture); this.#listeners = []; } /** * Gets an array of currently held key names. * * @returns Array of key names currently being pressed */ getHeldKeys() { return this.store.state.heldKeys; } /** * Checks if a specific key is currently being held. * * Letters match in any case. Other key names are case-sensitive, so * `'escape'` doesn't match `Escape`. Every adapter's key-hold helper uses * this rule. * * @param key - The key name to check, such as `'A'`, `'Shift'`, or `'Escape'` * @returns True if the key is currently held */ isKeyHeld(key) { const normalizedKey = normalizeKeyName(key); return Array.from(this.#heldEntries.values()).some((entry) => entry.key === normalizedKey); } /** * Whether a lock key is on. Returns `null` while no browser event has * reported it: until the user presses a key, clicks, or moves the mouse, * after the window loses focus or the page is hidden, and always for Num Lock and Scroll Lock on * macOS and iOS. * * @param key - `'CapsLock'`, `'NumLock'`, or `'ScrollLock'` * @returns `true` when the lock is on, `false` when it is off, or `null` */ getKeyLockState(key) { return this.store.state.locks[key]; } /** * Checks if any of the given keys are currently held. * * @param keys - Array of key names to check * @returns True if any of the keys are currently held */ isAnyKeyHeld(keys) { return keys.some((key) => this.isKeyHeld(key)); } /** * Checks if all of the given keys are currently held. * * @param keys - Array of key names to check * @returns True if all of the keys are currently held */ areAllKeysHeld(keys) { return keys.every((key) => this.isKeyHeld(key)); } /** * Destroys the tracker and removes all listeners. */ destroy() { this.#removeListeners(); this.#heldEntries.clear(); this.#locks = unknownLocks(); this.store.setState(() => getDefaultKeyStateTrackerState()); } }; /** * Gets the singleton KeyStateTracker instance. * Convenience function for accessing the tracker. */ function getKeyStateTracker() { return KeyStateTracker.getInstance(); } /** * Whether a key event names no key at all. Chrome autofill sends a trusted * plain `Event` named keydown or keyup, without modifier flags, so it says * nothing about which keys are held. */ function isKeyless(event) { return !event.key && !event.code; } export { KeyStateTracker, getKeyStateTracker };