UNPKG

@tanstack/hotkeys

Version:

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

125 lines • 4.22 kB
import { Store } from "./_store.js"; /** * State interface for the KeyStateTracker. */ export interface KeyStateTrackerState { /** * Array of currently held key names (normalized, e.g. "Control", "A"). */ heldKeys: Array<string>; /** * Map from normalized key name to the physical `event.code` (e.g. "KeyA", "ShiftLeft"). * Useful for debugging which physical key was pressed. */ heldCodes: Record<string, string>; /** * Whether each lock key is on. `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, where browsers don't report them. */ locks: Record<LockKey, boolean | null>; } /** A lock key that browsers report through `getModifierState()`. */ export type LockKey = 'CapsLock' | 'NumLock' | 'ScrollLock'; /** * 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() * ``` */ export declare class KeyStateTracker { #private; /** * The TanStack Store instance containing the tracker state. * Use this to subscribe to state changes or access current state. */ readonly store: Store<KeyStateTrackerState>; private constructor(); /** * Gets the singleton instance of KeyStateTracker. */ static getInstance(): KeyStateTracker; /** * Resets the singleton instance. Useful for testing. */ static resetInstance(): void; /** * Gets an array of currently held key names. * * @returns Array of key names currently being pressed */ getHeldKeys(): Array<string>; /** * 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: string): boolean; /** * 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: LockKey): boolean | null; /** * 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: Array<string>): boolean; /** * 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: Array<string>): boolean; /** * Destroys the tracker and removes all listeners. */ destroy(): void; } /** * Gets the singleton KeyStateTracker instance. * Convenience function for accessing the tracker. */ export declare function getKeyStateTracker(): KeyStateTracker;