@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
76 lines • 3.13 kB
TypeScript
import { CanonicalModifier, ResolvableModifier } from "./key.types.js";
/**
* Canonical order for modifiers in normalized hotkey strings.
*
* Defines the standard order in which modifiers should appear when formatting
* hotkeys. This ensures consistent, predictable output across the library.
*
* Order: Control → Alt → Shift → Meta
*
* @example
* ```ts
* // Input: 'Shift+Control+Meta+S'
* // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER)
* ```
*/
export declare const MODIFIER_ORDER: Array<CanonicalModifier>;
/**
* Set of canonical modifier key names for fast lookup.
*
* Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when
* a modifier is released so we can clear non-modifier keys whose keyup events
* may have been swallowed by the OS (e.g. macOS Cmd+key combos).
*/
export declare const MODIFIER_KEYS: Set<string>;
/**
* Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'.
*
* This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option')
* which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the
* platform-adaptive 'Mod' token.
*
* The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()`
* based on the detected platform (Command on Mac, Control elsewhere).
*
* @remarks Case-insensitive lookups are supported via lowercase variants
*
* @example
* ```ts
* MODIFIER_ALIASES['Ctrl'] // 'Control'
* MODIFIER_ALIASES['Cmd'] // 'Meta'
* MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime)
* ```
*/
export declare const MODIFIER_ALIASES: Record<string, ResolvableModifier>;
/**
* Maps `KeyboardEvent.code` values for punctuation keys to their canonical characters.
*
* On macOS, holding the Option (Alt) key transforms punctuation keys into special characters
* (e.g., Option+Minus → en-dash '–'), causing `event.key` to differ from the expected character.
* However, `event.code` still reports the physical key (e.g., 'Minus'). This map enables
* falling back to `event.code` for punctuation keys, similar to the existing `Key*`/`Digit*`
* fallbacks for letters and digits.
*/
export declare const PUNCTUATION_CODE_MAP: Record<string, string>;
/** Whether a key is a single Unicode letter, such as `'a'` or `'é'`. */
export declare function isSingleLetterKey(key: string): boolean;
/**
* Normalizes a key name from a `KeyboardEvent` or a hotkey: single letters
* become uppercase, and the space character becomes `Space`. Other names are
* returned as written, because key names are case-sensitive.
*
* Turkish dotless `ı` stays `ı`. Its uppercase is ASCII `I`, the uppercase of
* `i`, so uppercasing it would merge two different letters.
*
* @param key - The key name to normalize
* @returns The canonical key name
*
* @example
* ```ts
* normalizeKeyName('a') // 'A'
* normalizeKeyName('ı') // 'ı' (Turkish dotless i)
* normalizeKeyName(' ') // 'Space'
* normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)
* ```
*/
export declare function normalizeKeyName(key: string): string;