@tanstack/hotkeys 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +189 -45
  3. package/dist/constants.cjs +444 -0
  4. package/dist/constants.cjs.map +1 -0
  5. package/dist/constants.d.cts +226 -0
  6. package/dist/constants.d.ts +226 -0
  7. package/dist/constants.js +428 -0
  8. package/dist/constants.js.map +1 -0
  9. package/dist/format.cjs +178 -0
  10. package/dist/format.cjs.map +1 -0
  11. package/dist/format.d.cts +110 -0
  12. package/dist/format.d.ts +110 -0
  13. package/dist/format.js +175 -0
  14. package/dist/format.js.map +1 -0
  15. package/dist/hotkey-manager.cjs +401 -0
  16. package/dist/hotkey-manager.cjs.map +1 -0
  17. package/dist/hotkey-manager.d.cts +207 -0
  18. package/dist/hotkey-manager.d.ts +207 -0
  19. package/dist/hotkey-manager.js +400 -0
  20. package/dist/hotkey-manager.js.map +1 -0
  21. package/dist/hotkey.d.cts +278 -0
  22. package/dist/hotkey.d.ts +278 -0
  23. package/dist/index.cjs +54 -0
  24. package/dist/index.d.cts +11 -0
  25. package/dist/index.d.ts +11 -0
  26. package/dist/index.js +11 -0
  27. package/dist/key-state-tracker.cjs +197 -0
  28. package/dist/key-state-tracker.cjs.map +1 -0
  29. package/dist/key-state-tracker.d.cts +107 -0
  30. package/dist/key-state-tracker.d.ts +107 -0
  31. package/dist/key-state-tracker.js +196 -0
  32. package/dist/key-state-tracker.js.map +1 -0
  33. package/dist/match.cjs +143 -0
  34. package/dist/match.cjs.map +1 -0
  35. package/dist/match.d.cts +79 -0
  36. package/dist/match.d.ts +79 -0
  37. package/dist/match.js +141 -0
  38. package/dist/match.js.map +1 -0
  39. package/dist/parse.cjs +266 -0
  40. package/dist/parse.cjs.map +1 -0
  41. package/dist/parse.d.cts +169 -0
  42. package/dist/parse.d.ts +169 -0
  43. package/dist/parse.js +258 -0
  44. package/dist/parse.js.map +1 -0
  45. package/dist/recorder.cjs +177 -0
  46. package/dist/recorder.cjs.map +1 -0
  47. package/dist/recorder.d.cts +108 -0
  48. package/dist/recorder.d.ts +108 -0
  49. package/dist/recorder.js +177 -0
  50. package/dist/recorder.js.map +1 -0
  51. package/dist/sequence.cjs +242 -0
  52. package/dist/sequence.cjs.map +1 -0
  53. package/dist/sequence.d.cts +109 -0
  54. package/dist/sequence.d.ts +109 -0
  55. package/dist/sequence.js +240 -0
  56. package/dist/sequence.js.map +1 -0
  57. package/dist/validate.cjs +116 -0
  58. package/dist/validate.cjs.map +1 -0
  59. package/dist/validate.d.cts +56 -0
  60. package/dist/validate.d.ts +56 -0
  61. package/dist/validate.js +114 -0
  62. package/dist/validate.js.map +1 -0
  63. package/package.json +55 -7
  64. package/src/constants.ts +514 -0
  65. package/src/format.ts +261 -0
  66. package/src/hotkey-manager.ts +798 -0
  67. package/src/hotkey.ts +411 -0
  68. package/src/index.ts +10 -0
  69. package/src/key-state-tracker.ts +249 -0
  70. package/src/match.ts +222 -0
  71. package/src/parse.ts +368 -0
  72. package/src/recorder.ts +266 -0
  73. package/src/sequence.ts +391 -0
  74. package/src/validate.ts +171 -0
@@ -0,0 +1,444 @@
1
+
2
+ //#region src/constants.ts
3
+ /**
4
+ * Detects the current platform based on browser navigator properties.
5
+ *
6
+ * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
7
+ * Control elsewhere) and for platform-specific hotkey formatting.
8
+ *
9
+ * @returns The detected platform: 'mac', 'windows', or 'linux'
10
+ * @remarks Defaults to 'linux' in SSR environments where navigator is undefined
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
15
+ * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
16
+ * ```
17
+ */
18
+ function detectPlatform() {
19
+ if (typeof navigator === "undefined") return "linux";
20
+ const platform = navigator.platform.toLowerCase();
21
+ const userAgent = navigator.userAgent.toLowerCase();
22
+ if (platform.includes("mac") || userAgent.includes("mac")) return "mac";
23
+ if (platform.includes("win") || userAgent.includes("win")) return "windows";
24
+ return "linux";
25
+ }
26
+ /**
27
+ * Canonical order for modifiers in normalized hotkey strings.
28
+ *
29
+ * Defines the standard order in which modifiers should appear when formatting
30
+ * hotkeys. This ensures consistent, predictable output across the library.
31
+ *
32
+ * Order: Control → Alt → Shift → Meta
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * // Input: 'Shift+Control+Meta+S'
37
+ * // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER)
38
+ * ```
39
+ */
40
+ const MODIFIER_ORDER = [
41
+ "Control",
42
+ "Alt",
43
+ "Shift",
44
+ "Meta"
45
+ ];
46
+ /**
47
+ * Set of canonical modifier key names for fast lookup.
48
+ *
49
+ * Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when
50
+ * a modifier is released so we can clear non-modifier keys whose keyup events
51
+ * may have been swallowed by the OS (e.g. macOS Cmd+key combos).
52
+ */
53
+ const MODIFIER_KEYS = new Set(MODIFIER_ORDER);
54
+ /**
55
+ * Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'.
56
+ *
57
+ * This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option')
58
+ * which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the
59
+ * platform-adaptive 'Mod' token.
60
+ *
61
+ * The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()`
62
+ * based on the detected platform (Command on Mac, Control elsewhere).
63
+ *
64
+ * @remarks Case-insensitive lookups are supported via lowercase variants
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * MODIFIER_ALIASES['Ctrl'] // 'Control'
69
+ * MODIFIER_ALIASES['Cmd'] // 'Meta'
70
+ * MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime)
71
+ * ```
72
+ */
73
+ const MODIFIER_ALIASES = {
74
+ Control: "Control",
75
+ Ctrl: "Control",
76
+ control: "Control",
77
+ ctrl: "Control",
78
+ Shift: "Shift",
79
+ shift: "Shift",
80
+ Alt: "Alt",
81
+ Option: "Alt",
82
+ alt: "Alt",
83
+ option: "Alt",
84
+ Command: "Meta",
85
+ Cmd: "Meta",
86
+ Meta: "Meta",
87
+ command: "Meta",
88
+ cmd: "Meta",
89
+ meta: "Meta",
90
+ CommandOrControl: "Mod",
91
+ Mod: "Mod",
92
+ commandorcontrol: "Mod",
93
+ mod: "Mod"
94
+ };
95
+ /**
96
+ * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
97
+ *
98
+ * The 'Mod' token represents the "primary modifier" on each platform:
99
+ * - macOS: 'Meta' (Command key ⌘)
100
+ * - Windows/Linux: 'Control' (Ctrl key)
101
+ *
102
+ * This enables cross-platform hotkey definitions like 'Mod+S' that automatically
103
+ * map to Command+S on Mac and Ctrl+S on Windows/Linux.
104
+ *
105
+ * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
106
+ * @param platform - The target platform. Defaults to auto-detection.
107
+ * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * resolveModifier('Mod', 'mac') // 'Meta'
112
+ * resolveModifier('Mod', 'windows') // 'Control'
113
+ * resolveModifier('Control', 'mac') // 'Control' (unchanged)
114
+ * ```
115
+ */
116
+ function resolveModifier(modifier, platform = detectPlatform()) {
117
+ if (modifier === "Mod") return platform === "mac" ? "Meta" : "Control";
118
+ return modifier;
119
+ }
120
+ /**
121
+ * Set of all valid letter keys (A-Z).
122
+ *
123
+ * Used for validation and type checking. Letter keys are matched case-insensitively
124
+ * in hotkey matching, but normalized to uppercase in canonical form.
125
+ */
126
+ const LETTER_KEYS = new Set([
127
+ "A",
128
+ "B",
129
+ "C",
130
+ "D",
131
+ "E",
132
+ "F",
133
+ "G",
134
+ "H",
135
+ "I",
136
+ "J",
137
+ "K",
138
+ "L",
139
+ "M",
140
+ "N",
141
+ "O",
142
+ "P",
143
+ "Q",
144
+ "R",
145
+ "S",
146
+ "T",
147
+ "U",
148
+ "V",
149
+ "W",
150
+ "X",
151
+ "Y",
152
+ "Z"
153
+ ]);
154
+ /**
155
+ * Set of all valid number keys (0-9).
156
+ *
157
+ * Note: Number keys are affected by Shift (Shift+1 → '!' on US layout),
158
+ * so they're excluded from Shift-based hotkey combinations to avoid
159
+ * layout-dependent behavior.
160
+ */
161
+ const NUMBER_KEYS = new Set([
162
+ "0",
163
+ "1",
164
+ "2",
165
+ "3",
166
+ "4",
167
+ "5",
168
+ "6",
169
+ "7",
170
+ "8",
171
+ "9"
172
+ ]);
173
+ /**
174
+ * Set of all valid function keys (F1-F12).
175
+ *
176
+ * Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,
177
+ * Alt+F4 to close windows) and application-specific commands.
178
+ */
179
+ const FUNCTION_KEYS = new Set([
180
+ "F1",
181
+ "F2",
182
+ "F3",
183
+ "F4",
184
+ "F5",
185
+ "F6",
186
+ "F7",
187
+ "F8",
188
+ "F9",
189
+ "F10",
190
+ "F11",
191
+ "F12"
192
+ ]);
193
+ /**
194
+ * Set of all valid navigation keys for cursor movement and document navigation.
195
+ *
196
+ * Includes arrow keys, Home/End (line navigation), and PageUp/PageDown (page navigation).
197
+ * These keys are commonly combined with modifiers for selection (Shift+ArrowUp) or
198
+ * navigation shortcuts (Alt+ArrowLeft for back).
199
+ */
200
+ const NAVIGATION_KEYS = new Set([
201
+ "ArrowUp",
202
+ "ArrowDown",
203
+ "ArrowLeft",
204
+ "ArrowRight",
205
+ "Home",
206
+ "End",
207
+ "PageUp",
208
+ "PageDown"
209
+ ]);
210
+ /**
211
+ * Set of all valid editing and special keys.
212
+ *
213
+ * Includes keys commonly used for text editing (Enter, Backspace, Delete, Tab) and
214
+ * special actions (Escape, Space). These keys are frequently combined with modifiers
215
+ * for editor shortcuts (Mod+Enter to submit, Shift+Tab to go back).
216
+ */
217
+ const EDITING_KEYS = new Set([
218
+ "Enter",
219
+ "Escape",
220
+ "Space",
221
+ "Tab",
222
+ "Backspace",
223
+ "Delete"
224
+ ]);
225
+ /**
226
+ * Set of all valid punctuation keys commonly used in keyboard shortcuts.
227
+ *
228
+ * These are the literal characters as they appear in `KeyboardEvent.key` (layout-dependent,
229
+ * typically US keyboard layout). Common shortcuts include:
230
+ * - `Mod+/` - Toggle comment
231
+ * - `Mod+[` / `Mod+]` - Indent/outdent
232
+ * - `Mod+=` / `Mod+-` - Zoom in/out
233
+ *
234
+ * Note: Punctuation keys are affected by Shift (Shift+',' → '<' on US layout),
235
+ * so they're excluded from Shift-based hotkey combinations to avoid layout-dependent behavior.
236
+ */
237
+ const PUNCTUATION_KEYS = new Set([
238
+ "/",
239
+ "[",
240
+ "]",
241
+ "\\",
242
+ "=",
243
+ "-",
244
+ ",",
245
+ ".",
246
+ "`"
247
+ ]);
248
+ /**
249
+ * Set of all valid non-modifier keys.
250
+ *
251
+ * This is the union of all key type sets (letters, numbers, function keys, navigation,
252
+ * editing, and punctuation). Used primarily for validation to check if a key string
253
+ * is recognized and will have type-safe autocomplete support.
254
+ *
255
+ * @see {@link LETTER_KEYS}
256
+ * @see {@link NUMBER_KEYS}
257
+ * @see {@link FUNCTION_KEYS}
258
+ * @see {@link NAVIGATION_KEYS}
259
+ * @see {@link EDITING_KEYS}
260
+ * @see {@link PUNCTUATION_KEYS}
261
+ */
262
+ const ALL_KEYS = new Set([
263
+ ...LETTER_KEYS,
264
+ ...NUMBER_KEYS,
265
+ ...FUNCTION_KEYS,
266
+ ...NAVIGATION_KEYS,
267
+ ...EDITING_KEYS,
268
+ ...PUNCTUATION_KEYS
269
+ ]);
270
+ /**
271
+ * Maps key name aliases to their canonical form.
272
+ *
273
+ * Handles common variations and alternative names for keys to provide a more
274
+ * forgiving API. For example, users can write 'Esc', 'esc', or 'escape' and
275
+ * they'll all normalize to 'Escape'.
276
+ *
277
+ * This map is used internally by `normalizeKeyName()` to convert user input
278
+ * into the canonical key names used throughout the library.
279
+ *
280
+ * @remarks Case-insensitive lookups are supported via lowercase variants
281
+ *
282
+ * @example
283
+ * ```ts
284
+ * KEY_ALIASES['Esc'] // 'Escape'
285
+ * KEY_ALIASES['Del'] // 'Delete'
286
+ * KEY_ALIASES['Up'] // 'ArrowUp'
287
+ * ```
288
+ */
289
+ const KEY_ALIASES = {
290
+ Esc: "Escape",
291
+ esc: "Escape",
292
+ escape: "Escape",
293
+ Return: "Enter",
294
+ return: "Enter",
295
+ enter: "Enter",
296
+ " ": "Space",
297
+ space: "Space",
298
+ Spacebar: "Space",
299
+ spacebar: "Space",
300
+ tab: "Tab",
301
+ backspace: "Backspace",
302
+ Del: "Delete",
303
+ del: "Delete",
304
+ delete: "Delete",
305
+ Up: "ArrowUp",
306
+ up: "ArrowUp",
307
+ arrowup: "ArrowUp",
308
+ Down: "ArrowDown",
309
+ down: "ArrowDown",
310
+ arrowdown: "ArrowDown",
311
+ Left: "ArrowLeft",
312
+ left: "ArrowLeft",
313
+ arrowleft: "ArrowLeft",
314
+ Right: "ArrowRight",
315
+ right: "ArrowRight",
316
+ arrowright: "ArrowRight",
317
+ home: "Home",
318
+ end: "End",
319
+ pageup: "PageUp",
320
+ pagedown: "PageDown",
321
+ PgUp: "PageUp",
322
+ PgDn: "PageDown",
323
+ pgup: "PageUp",
324
+ pgdn: "PageDown"
325
+ };
326
+ /**
327
+ * Normalizes a key name to its canonical form.
328
+ *
329
+ * Converts various key name formats (aliases, case variations) into the standard
330
+ * canonical names used throughout the library. This enables a more forgiving API
331
+ * where users can write keys in different ways and still get correct behavior.
332
+ *
333
+ * Normalization rules:
334
+ * 1. Check aliases first (e.g., 'Esc' → 'Escape', 'Del' → 'Delete')
335
+ * 2. Single letters → uppercase (e.g., 'a' → 'A', 's' → 'S')
336
+ * 3. Function keys → uppercase (e.g., 'f1' → 'F1', 'F12' → 'F12')
337
+ * 4. Other keys → returned as-is (already canonical or unknown)
338
+ *
339
+ * @param key - The key name to normalize (can be an alias, lowercase, etc.)
340
+ * @returns The canonical key name
341
+ *
342
+ * @example
343
+ * ```ts
344
+ * normalizeKeyName('esc') // 'Escape'
345
+ * normalizeKeyName('a') // 'A'
346
+ * normalizeKeyName('f1') // 'F1'
347
+ * normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)
348
+ * ```
349
+ */
350
+ function normalizeKeyName(key) {
351
+ if (key in KEY_ALIASES) return KEY_ALIASES[key];
352
+ if (key.length === 1 && /^[a-zA-Z]$/.test(key)) return key.toUpperCase();
353
+ const upperKey = key.toUpperCase();
354
+ if (/^F([1-9]|1[0-2])$/.test(upperKey)) return upperKey;
355
+ return key;
356
+ }
357
+ /**
358
+ * Modifier key symbols for macOS display.
359
+ *
360
+ * Used by formatting functions to display hotkeys with macOS-style symbols
361
+ * (e.g., ⌘ for Command, ⌃ for Control) instead of text labels. This provides
362
+ * a native macOS look and feel in hotkey displays.
363
+ *
364
+ * @example
365
+ * ```ts
366
+ * MAC_MODIFIER_SYMBOLS['Meta'] // '⌘'
367
+ * MAC_MODIFIER_SYMBOLS['Control'] // '⌃'
368
+ * MAC_MODIFIER_SYMBOLS['Alt'] // '⌥'
369
+ * MAC_MODIFIER_SYMBOLS['Shift'] // '⇧'
370
+ * ```
371
+ */
372
+ const MAC_MODIFIER_SYMBOLS = {
373
+ Control: "⌃",
374
+ Alt: "⌥",
375
+ Shift: "⇧",
376
+ Meta: "⌘"
377
+ };
378
+ /**
379
+ * Modifier key labels for Windows/Linux display.
380
+ *
381
+ * Used by formatting functions to display hotkeys with standard text labels
382
+ * (e.g., 'Ctrl' for Control, 'Win' for Meta/Windows key) instead of symbols.
383
+ * This provides a familiar Windows/Linux look and feel in hotkey displays.
384
+ *
385
+ * @example
386
+ * ```ts
387
+ * STANDARD_MODIFIER_LABELS['Control'] // 'Ctrl'
388
+ * STANDARD_MODIFIER_LABELS['Meta'] // 'Win'
389
+ * STANDARD_MODIFIER_LABELS['Alt'] // 'Alt'
390
+ * STANDARD_MODIFIER_LABELS['Shift'] // 'Shift'
391
+ * ```
392
+ */
393
+ const STANDARD_MODIFIER_LABELS = {
394
+ Control: "Ctrl",
395
+ Alt: "Alt",
396
+ Shift: "Shift",
397
+ Meta: "Win"
398
+ };
399
+ /**
400
+ * Special key symbols for display formatting.
401
+ *
402
+ * Maps certain keys to their visual symbols for better readability in hotkey displays.
403
+ * Used by formatting functions to show symbols like ↑ for ArrowUp or ↵ for Enter
404
+ * instead of text labels.
405
+ *
406
+ * @example
407
+ * ```ts
408
+ * KEY_DISPLAY_SYMBOLS['ArrowUp'] // '↑'
409
+ * KEY_DISPLAY_SYMBOLS['Enter'] // '↵'
410
+ * KEY_DISPLAY_SYMBOLS['Escape'] // 'Esc'
411
+ * KEY_DISPLAY_SYMBOLS['Space'] // '␣'
412
+ * ```
413
+ */
414
+ const KEY_DISPLAY_SYMBOLS = {
415
+ ArrowUp: "↑",
416
+ ArrowDown: "↓",
417
+ ArrowLeft: "←",
418
+ ArrowRight: "→",
419
+ Enter: "↵",
420
+ Escape: "Esc",
421
+ Backspace: "⌫",
422
+ Delete: "⌦",
423
+ Tab: "⇥",
424
+ Space: "␣"
425
+ };
426
+
427
+ //#endregion
428
+ exports.ALL_KEYS = ALL_KEYS;
429
+ exports.EDITING_KEYS = EDITING_KEYS;
430
+ exports.FUNCTION_KEYS = FUNCTION_KEYS;
431
+ exports.KEY_DISPLAY_SYMBOLS = KEY_DISPLAY_SYMBOLS;
432
+ exports.LETTER_KEYS = LETTER_KEYS;
433
+ exports.MAC_MODIFIER_SYMBOLS = MAC_MODIFIER_SYMBOLS;
434
+ exports.MODIFIER_ALIASES = MODIFIER_ALIASES;
435
+ exports.MODIFIER_KEYS = MODIFIER_KEYS;
436
+ exports.MODIFIER_ORDER = MODIFIER_ORDER;
437
+ exports.NAVIGATION_KEYS = NAVIGATION_KEYS;
438
+ exports.NUMBER_KEYS = NUMBER_KEYS;
439
+ exports.PUNCTUATION_KEYS = PUNCTUATION_KEYS;
440
+ exports.STANDARD_MODIFIER_LABELS = STANDARD_MODIFIER_LABELS;
441
+ exports.detectPlatform = detectPlatform;
442
+ exports.normalizeKeyName = normalizeKeyName;
443
+ exports.resolveModifier = resolveModifier;
444
+ //# sourceMappingURL=constants.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.cjs","names":[],"sources":["../src/constants.ts"],"sourcesContent":["import type {\n CanonicalModifier,\n EditingKey,\n FunctionKey,\n LetterKey,\n NavigationKey,\n NumberKey,\n PunctuationKey,\n} from './hotkey'\n\n// =============================================================================\n// Platform Detection\n// =============================================================================\n\n/**\n * Detects the current platform based on browser navigator properties.\n *\n * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,\n * Control elsewhere) and for platform-specific hotkey formatting.\n *\n * @returns The detected platform: 'mac', 'windows', or 'linux'\n * @remarks Defaults to 'linux' in SSR environments where navigator is undefined\n *\n * @example\n * ```ts\n * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'\n * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere\n * ```\n */\nexport function detectPlatform(): 'mac' | 'windows' | 'linux' {\n if (typeof navigator === 'undefined') {\n return 'linux' // Default for SSR\n }\n\n const platform = navigator.platform.toLowerCase()\n const userAgent = navigator.userAgent.toLowerCase()\n\n if (platform.includes('mac') || userAgent.includes('mac')) {\n return 'mac'\n }\n if (platform.includes('win') || userAgent.includes('win')) {\n return 'windows'\n }\n return 'linux'\n}\n\n// =============================================================================\n// Modifier Aliases\n// =============================================================================\n\n/**\n * Canonical order for modifiers in normalized hotkey strings.\n *\n * Defines the standard order in which modifiers should appear when formatting\n * hotkeys. This ensures consistent, predictable output across the library.\n *\n * Order: Control → Alt → Shift → Meta\n *\n * @example\n * ```ts\n * // Input: 'Shift+Control+Meta+S'\n * // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER)\n * ```\n */\nexport const MODIFIER_ORDER: Array<CanonicalModifier> = [\n 'Control',\n 'Alt',\n 'Shift',\n 'Meta',\n]\n\n/**\n * Set of canonical modifier key names for fast lookup.\n *\n * Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when\n * a modifier is released so we can clear non-modifier keys whose keyup events\n * may have been swallowed by the OS (e.g. macOS Cmd+key combos).\n */\nexport const MODIFIER_KEYS = new Set<string>(MODIFIER_ORDER)\n\n/**\n * Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'.\n *\n * This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option')\n * which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the\n * platform-adaptive 'Mod' token.\n *\n * The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()`\n * based on the detected platform (Command on Mac, Control elsewhere).\n *\n * @remarks Case-insensitive lookups are supported via lowercase variants\n *\n * @example\n * ```ts\n * MODIFIER_ALIASES['Ctrl'] // 'Control'\n * MODIFIER_ALIASES['Cmd'] // 'Meta'\n * MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime)\n * ```\n */\nexport const MODIFIER_ALIASES: Record<string, CanonicalModifier | 'Mod'> = {\n // Control variants\n Control: 'Control',\n Ctrl: 'Control',\n control: 'Control',\n ctrl: 'Control',\n\n // Shift variants\n Shift: 'Shift',\n shift: 'Shift',\n\n // Alt variants\n Alt: 'Alt',\n Option: 'Alt',\n alt: 'Alt',\n option: 'Alt',\n\n // Meta/Command variants\n Command: 'Meta',\n Cmd: 'Meta',\n Meta: 'Meta',\n command: 'Meta',\n cmd: 'Meta',\n meta: 'Meta',\n\n // Platform-adaptive (resolved at runtime)\n CommandOrControl: 'Mod',\n Mod: 'Mod',\n commandorcontrol: 'Mod',\n mod: 'Mod',\n}\n\n/**\n * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.\n *\n * The 'Mod' token represents the \"primary modifier\" on each platform:\n * - macOS: 'Meta' (Command key ⌘)\n * - Windows/Linux: 'Control' (Ctrl key)\n *\n * This enables cross-platform hotkey definitions like 'Mod+S' that automatically\n * map to Command+S on Mac and Ctrl+S on Windows/Linux.\n *\n * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.\n * @param platform - The target platform. Defaults to auto-detection.\n * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')\n *\n * @example\n * ```ts\n * resolveModifier('Mod', 'mac') // 'Meta'\n * resolveModifier('Mod', 'windows') // 'Control'\n * resolveModifier('Control', 'mac') // 'Control' (unchanged)\n * ```\n */\nexport function resolveModifier(\n modifier: CanonicalModifier | 'Mod',\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): CanonicalModifier {\n if (modifier === 'Mod') {\n return platform === 'mac' ? 'Meta' : 'Control'\n }\n return modifier\n}\n\n// =============================================================================\n// Valid Keys\n// =============================================================================\n\n/**\n * Set of all valid letter keys (A-Z).\n *\n * Used for validation and type checking. Letter keys are matched case-insensitively\n * in hotkey matching, but normalized to uppercase in canonical form.\n */\nexport const LETTER_KEYS = new Set<LetterKey>([\n 'A',\n 'B',\n 'C',\n 'D',\n 'E',\n 'F',\n 'G',\n 'H',\n 'I',\n 'J',\n 'K',\n 'L',\n 'M',\n 'N',\n 'O',\n 'P',\n 'Q',\n 'R',\n 'S',\n 'T',\n 'U',\n 'V',\n 'W',\n 'X',\n 'Y',\n 'Z',\n])\n\n/**\n * Set of all valid number keys (0-9).\n *\n * Note: Number keys are affected by Shift (Shift+1 → '!' on US layout),\n * so they're excluded from Shift-based hotkey combinations to avoid\n * layout-dependent behavior.\n */\nexport const NUMBER_KEYS = new Set<NumberKey>([\n '0',\n '1',\n '2',\n '3',\n '4',\n '5',\n '6',\n '7',\n '8',\n '9',\n])\n\n/**\n * Set of all valid function keys (F1-F12).\n *\n * Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,\n * Alt+F4 to close windows) and application-specific commands.\n */\nexport const FUNCTION_KEYS = new Set<FunctionKey>([\n 'F1',\n 'F2',\n 'F3',\n 'F4',\n 'F5',\n 'F6',\n 'F7',\n 'F8',\n 'F9',\n 'F10',\n 'F11',\n 'F12',\n])\n\n/**\n * Set of all valid navigation keys for cursor movement and document navigation.\n *\n * Includes arrow keys, Home/End (line navigation), and PageUp/PageDown (page navigation).\n * These keys are commonly combined with modifiers for selection (Shift+ArrowUp) or\n * navigation shortcuts (Alt+ArrowLeft for back).\n */\nexport const NAVIGATION_KEYS = new Set<NavigationKey>([\n 'ArrowUp',\n 'ArrowDown',\n 'ArrowLeft',\n 'ArrowRight',\n 'Home',\n 'End',\n 'PageUp',\n 'PageDown',\n])\n\n/**\n * Set of all valid editing and special keys.\n *\n * Includes keys commonly used for text editing (Enter, Backspace, Delete, Tab) and\n * special actions (Escape, Space). These keys are frequently combined with modifiers\n * for editor shortcuts (Mod+Enter to submit, Shift+Tab to go back).\n */\nexport const EDITING_KEYS = new Set<EditingKey>([\n 'Enter',\n 'Escape',\n 'Space',\n 'Tab',\n 'Backspace',\n 'Delete',\n])\n\n/**\n * Set of all valid punctuation keys commonly used in keyboard shortcuts.\n *\n * These are the literal characters as they appear in `KeyboardEvent.key` (layout-dependent,\n * typically US keyboard layout). Common shortcuts include:\n * - `Mod+/` - Toggle comment\n * - `Mod+[` / `Mod+]` - Indent/outdent\n * - `Mod+=` / `Mod+-` - Zoom in/out\n *\n * Note: Punctuation keys are affected by Shift (Shift+',' → '<' on US layout),\n * so they're excluded from Shift-based hotkey combinations to avoid layout-dependent behavior.\n */\nexport const PUNCTUATION_KEYS = new Set<PunctuationKey>([\n '/',\n '[',\n ']',\n '\\\\',\n '=',\n '-',\n ',',\n '.',\n '`',\n])\n\n/**\n * Set of all valid non-modifier keys.\n *\n * This is the union of all key type sets (letters, numbers, function keys, navigation,\n * editing, and punctuation). Used primarily for validation to check if a key string\n * is recognized and will have type-safe autocomplete support.\n *\n * @see {@link LETTER_KEYS}\n * @see {@link NUMBER_KEYS}\n * @see {@link FUNCTION_KEYS}\n * @see {@link NAVIGATION_KEYS}\n * @see {@link EDITING_KEYS}\n * @see {@link PUNCTUATION_KEYS}\n */\nexport const ALL_KEYS = new Set([\n ...LETTER_KEYS,\n ...NUMBER_KEYS,\n ...FUNCTION_KEYS,\n ...NAVIGATION_KEYS,\n ...EDITING_KEYS,\n ...PUNCTUATION_KEYS,\n])\n\n/**\n * Maps key name aliases to their canonical form.\n *\n * Handles common variations and alternative names for keys to provide a more\n * forgiving API. For example, users can write 'Esc', 'esc', or 'escape' and\n * they'll all normalize to 'Escape'.\n *\n * This map is used internally by `normalizeKeyName()` to convert user input\n * into the canonical key names used throughout the library.\n *\n * @remarks Case-insensitive lookups are supported via lowercase variants\n *\n * @example\n * ```ts\n * KEY_ALIASES['Esc'] // 'Escape'\n * KEY_ALIASES['Del'] // 'Delete'\n * KEY_ALIASES['Up'] // 'ArrowUp'\n * ```\n */\nconst KEY_ALIASES: Record<string, string> = {\n // Escape variants\n Esc: 'Escape',\n esc: 'Escape',\n escape: 'Escape',\n\n // Enter variants\n Return: 'Enter',\n return: 'Enter',\n enter: 'Enter',\n\n // Space variants\n ' ': 'Space',\n space: 'Space',\n Spacebar: 'Space',\n spacebar: 'Space',\n\n // Tab variants\n tab: 'Tab',\n\n // Backspace variants\n backspace: 'Backspace',\n\n // Delete variants\n Del: 'Delete',\n del: 'Delete',\n delete: 'Delete',\n\n // Arrow key variants\n Up: 'ArrowUp',\n up: 'ArrowUp',\n arrowup: 'ArrowUp',\n Down: 'ArrowDown',\n down: 'ArrowDown',\n arrowdown: 'ArrowDown',\n Left: 'ArrowLeft',\n left: 'ArrowLeft',\n arrowleft: 'ArrowLeft',\n Right: 'ArrowRight',\n right: 'ArrowRight',\n arrowright: 'ArrowRight',\n\n // Navigation variants\n home: 'Home',\n end: 'End',\n pageup: 'PageUp',\n pagedown: 'PageDown',\n PgUp: 'PageUp',\n PgDn: 'PageDown',\n pgup: 'PageUp',\n pgdn: 'PageDown',\n}\n\n/**\n * Normalizes a key name to its canonical form.\n *\n * Converts various key name formats (aliases, case variations) into the standard\n * canonical names used throughout the library. This enables a more forgiving API\n * where users can write keys in different ways and still get correct behavior.\n *\n * Normalization rules:\n * 1. Check aliases first (e.g., 'Esc' → 'Escape', 'Del' → 'Delete')\n * 2. Single letters → uppercase (e.g., 'a' → 'A', 's' → 'S')\n * 3. Function keys → uppercase (e.g., 'f1' → 'F1', 'F12' → 'F12')\n * 4. Other keys → returned as-is (already canonical or unknown)\n *\n * @param key - The key name to normalize (can be an alias, lowercase, etc.)\n * @returns The canonical key name\n *\n * @example\n * ```ts\n * normalizeKeyName('esc') // 'Escape'\n * normalizeKeyName('a') // 'A'\n * normalizeKeyName('f1') // 'F1'\n * normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)\n * ```\n */\nexport function normalizeKeyName(key: string): string {\n // Check aliases first\n if (key in KEY_ALIASES) {\n return KEY_ALIASES[key]!\n }\n\n // Check if it's a single letter (normalize to uppercase)\n if (key.length === 1 && /^[a-zA-Z]$/.test(key)) {\n return key.toUpperCase()\n }\n\n // Check if it's a function key (normalize case)\n const upperKey = key.toUpperCase()\n if (/^F([1-9]|1[0-2])$/.test(upperKey)) {\n return upperKey\n }\n\n return key\n}\n\n// =============================================================================\n// Display Symbols\n// =============================================================================\n\n/**\n * Modifier key symbols for macOS display.\n *\n * Used by formatting functions to display hotkeys with macOS-style symbols\n * (e.g., ⌘ for Command, ⌃ for Control) instead of text labels. This provides\n * a native macOS look and feel in hotkey displays.\n *\n * @example\n * ```ts\n * MAC_MODIFIER_SYMBOLS['Meta'] // '⌘'\n * MAC_MODIFIER_SYMBOLS['Control'] // '⌃'\n * MAC_MODIFIER_SYMBOLS['Alt'] // '⌥'\n * MAC_MODIFIER_SYMBOLS['Shift'] // '⇧'\n * ```\n */\nexport const MAC_MODIFIER_SYMBOLS: Record<CanonicalModifier, string> = {\n Control: '⌃',\n Alt: '⌥',\n Shift: '⇧',\n Meta: '⌘',\n}\n\n/**\n * Modifier key labels for Windows/Linux display.\n *\n * Used by formatting functions to display hotkeys with standard text labels\n * (e.g., 'Ctrl' for Control, 'Win' for Meta/Windows key) instead of symbols.\n * This provides a familiar Windows/Linux look and feel in hotkey displays.\n *\n * @example\n * ```ts\n * STANDARD_MODIFIER_LABELS['Control'] // 'Ctrl'\n * STANDARD_MODIFIER_LABELS['Meta'] // 'Win'\n * STANDARD_MODIFIER_LABELS['Alt'] // 'Alt'\n * STANDARD_MODIFIER_LABELS['Shift'] // 'Shift'\n * ```\n */\nexport const STANDARD_MODIFIER_LABELS: Record<CanonicalModifier, string> = {\n Control: 'Ctrl',\n Alt: 'Alt',\n Shift: 'Shift',\n Meta: 'Win',\n}\n\n/**\n * Special key symbols for display formatting.\n *\n * Maps certain keys to their visual symbols for better readability in hotkey displays.\n * Used by formatting functions to show symbols like ↑ for ArrowUp or ↵ for Enter\n * instead of text labels.\n *\n * @example\n * ```ts\n * KEY_DISPLAY_SYMBOLS['ArrowUp'] // '↑'\n * KEY_DISPLAY_SYMBOLS['Enter'] // '↵'\n * KEY_DISPLAY_SYMBOLS['Escape'] // 'Esc'\n * KEY_DISPLAY_SYMBOLS['Space'] // '␣'\n * ```\n */\nexport const KEY_DISPLAY_SYMBOLS: Record<string, string> = {\n ArrowUp: '↑',\n ArrowDown: '↓',\n ArrowLeft: '←',\n ArrowRight: '→',\n Enter: '↵',\n Escape: 'Esc',\n Backspace: '⌫',\n Delete: '⌦',\n Tab: '⇥',\n Space: '␣',\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA6BA,SAAgB,iBAA8C;AAC5D,KAAI,OAAO,cAAc,YACvB,QAAO;CAGT,MAAM,WAAW,UAAU,SAAS,aAAa;CACjD,MAAM,YAAY,UAAU,UAAU,aAAa;AAEnD,KAAI,SAAS,SAAS,MAAM,IAAI,UAAU,SAAS,MAAM,CACvD,QAAO;AAET,KAAI,SAAS,SAAS,MAAM,IAAI,UAAU,SAAS,MAAM,CACvD,QAAO;AAET,QAAO;;;;;;;;;;;;;;;;AAqBT,MAAa,iBAA2C;CACtD;CACA;CACA;CACA;CACD;;;;;;;;AASD,MAAa,gBAAgB,IAAI,IAAY,eAAe;;;;;;;;;;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