UNPKG

@tanstack/hotkeys

Version:

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

139 lines • 7.16 kB
import { LOGICAL_ONLY_NAMED_KEYS, SHARED_NAMED_KEYS } from "./_constants.js"; import { BuiltInLayoutKey } from "./key-sets/_all.js"; import { UsKeys } from "./key-sets/us.js"; import { Register } from "./register.js"; /** * All supported modifier key names, including aliases. * - Control/Ctrl: The Control key * - Shift: The Shift key * - Alt/Option: The Alt key (Option on macOS) * - Command/Cmd: The Command key (macOS only) * - CommandOrControl/Mod: Command on macOS, Control on other platforms */ export type Modifier = 'Control' | 'Ctrl' | 'Shift' | 'Alt' | 'Option' | 'Command' | 'Cmd' | 'CommandOrControl' | 'Mod'; /** * Canonical modifier names that map to KeyboardEvent properties. */ export type CanonicalModifier = 'Control' | 'Shift' | 'Alt' | 'Meta'; /** A canonical modifier, or `Mod`, which resolves per platform. */ export type ResolvableModifier = CanonicalModifier | 'Mod'; /** * Letter keys A-Z (case-insensitive in matching). */ export type LetterKey = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'; /** * Number keys 0-9. */ export type NumberKey = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'; /** * Function keys F1-F24, supported as both logical keys and physical codes. */ export type FunctionKey = 'F1' | 'F2' | 'F3' | 'F4' | 'F5' | 'F6' | 'F7' | 'F8' | 'F9' | 'F10' | 'F11' | 'F12' | 'F13' | 'F14' | 'F15' | 'F16' | 'F17' | 'F18' | 'F19' | 'F20' | 'F21' | 'F22' | 'F23' | 'F24'; /** * Navigation keys for cursor movement. */ export type NavigationKey = 'ArrowUp' | 'ArrowDown' | 'ArrowLeft' | 'ArrowRight' | 'Home' | 'End' | 'PageUp' | 'PageDown'; /** * Editing and special keys. */ export type EditingKey = 'Enter' | 'Escape' | 'Space' | 'Tab' | 'Backspace' | 'Delete'; /** * The symbols of the US QWERTY layout, such as `/`, `?`, and `+`. Hotkey * strings accept these when `Register['keys']` names no layouts. Matching * uses the character the key types, whichever layout produced it. */ export type SymbolKey = '/' | '[' | ']' | '\\' | '=' | '-' | ',' | '.' | ';' | '`' | "'" | '+' | '?' | '!' | '@' | '#' | '$' | '%' | '^' | '&' | '*' | '(' | ')' | '_' | '{' | '}' | '|' | ':' | '"' | '<' | '>' | '~'; /** Additional named logical keys, including media, browser, lock, and input-mode keys. */ export type NamedKey = (typeof SHARED_NAMED_KEYS)[number] | (typeof LOGICAL_ONLY_NAMED_KEYS)[number]; /** * The keys every layout accepts: A-Z, digits, and named keys such as `Tab`. * Symbols, and letters such as `Ä` from registered layouts, come from * `LayoutKey`. */ export type NonSymbolKey = LetterKey | NumberKey | EditingKey | NavigationKey | FunctionKey | NamedKey; /** * Supported physical KeyboardEvent.code names, independent of keyboard layout. * Letter, digit, and numpad names use finite template unions. Modifier codes * such as `ShiftLeft` are excluded, because modifier-only hotkeys aren't * supported. `Unidentified` is excluded because it cannot identify a physical key. * * @see https://www.w3.org/TR/uievents-code/ */ export type PhysicalKeyCode = `Key${LetterKey}` | `Digit${NumberKey}` | `Numpad${NumberKey}` | EditingKey | NavigationKey | FunctionKey | (typeof SHARED_NAMED_KEYS)[number] | 'Backquote' | 'Backslash' | 'BracketLeft' | 'BracketRight' | 'Comma' | 'Equal' | 'IntlBackslash' | 'IntlRo' | 'IntlYen' | 'Minus' | 'Period' | 'Quote' | 'Semicolon' | 'Slash' | `Lang${1 | 2 | 3 | 4 | 5}` | `Numpad${'Add' | 'Backspace' | 'Clear' | 'ClearEntry' | 'Comma' | 'Decimal' | 'Divide' | 'Enter' | 'Equal' | 'Hash' | 'MemoryAdd' | 'MemoryClear' | 'MemoryRecall' | 'MemoryStore' | 'MemorySubtract' | 'Multiply' | 'ParenLeft' | 'ParenRight' | 'Star' | 'Subtract'}` | 'LaunchApp1' | 'LaunchApp2' | 'MediaSelect' | 'Sleep' | 'Turbo' | 'Abort' | 'Resume' | 'Suspend'; /** * An explicit physical key token. All codes use brackets, including names such * as `[Enter]` that also exist as logical keys. Invalid code names are rejected * by TypeScript, and supported names are available in autocomplete. */ export type PhysicalKey = `[${PhysicalKeyCode}]`; /** * The printable keys, other than A-Z and 0-9, that hotkey strings accept: * the keys of the layouts in `Register['keys']`, or the US QWERTY keys when * none are registered. * * @example * ```ts * import type { FrenchKeys, GermanKeys } from '@tanstack/react-hotkeys' * * declare module '@tanstack/react-hotkeys' { * interface Register { * keys: GermanKeys | FrenchKeys * } * } * ``` */ export type LayoutKey = Register extends { keys: infer TKeys extends string; } ? TKeys : UsKeys; /** Supported logical key names, excluding bracketed physical-code syntax. */ export type LogicalKey = NonSymbolKey | LayoutKey; /** All supported logical keys and bracketed physical key tokens. */ export type Key = LogicalKey | PhysicalKey; /** * Letters in the layout key sets with no one-character capital: German `ß`, * Spanish and Portuguese `ª` and `º`, and the Hebrew letters. `BuiltInSymbolKey` * can't tell them from symbols by case. tests/key-sets.test.ts checks every * key set against a copy of this list, so update both together. */ type UncasedLetterKey = 'ß' | 'ª' | 'º' | 'א' | 'ב' | 'ג' | 'ד' | 'ה' | 'ו' | 'ז' | 'ח' | 'ט' | 'י' | 'כ' | 'ך' | 'ל' | 'מ' | 'ם' | 'נ' | 'ן' | 'ס' | 'ע' | 'פ' | 'ף' | 'צ' | 'ץ' | 'ק' | 'ר' | 'ש' | 'ת'; /** The keys of a key set that aren't letters, such as `/` and `§`. */ type SymbolOf<TKey extends string> = TKey extends unknown ? TKey extends UncasedLetterKey ? never : Uppercase<TKey> extends Lowercase<TKey> ? TKey : never : never; /** The symbols of the built-in key sets, such as `/`, `§` and `°`. */ type BuiltInSymbolKey = SymbolOf<BuiltInLayoutKey>; /** * The keys a hotkey string can combine with `Shift`: letters, digits, named * keys such as `Tab`, and physical keys. From `LayoutKey`, the symbols of the * built-in key sets are left out, so `Shift+Ä` is allowed and `Shift+§` isn't. * A key from a custom key set that no built-in set has, such as Arabic `ا`, is * allowed. * * Symbols are left out, because Shift * changes which symbol a key types and layouts put symbols on different keys: * `Shift+/` would fire on `?` on a US keyboard and on `/` on a German one. * Write the symbol Shift types, such as `?`, or name the physical key, such as * `Shift+[Slash]`. The rule holds with every other modifier too: write * `Mod+?`, not `Mod+Shift+/`. */ export type ShiftableKey = NonSymbolKey | Exclude<LayoutKey, BuiltInSymbolKey> | PhysicalKey; /** * Includes both modifier keys and regular keys. */ export type IndividualKey = CanonicalModifier | Key; /** The key name and physical code of one key press. */ export interface KeyIdentity { key: string; code: string; } /** Keyboard event properties normalized for internal matching and recording. */ export interface NormalizedKeyboardEvent { key: string; code: string; ctrl: boolean; shift: boolean; alt: boolean; meta: boolean; altGraph: boolean; location: number; isComposing: boolean; } export {};