@tanstack/hotkeys 0.0.1 → 0.1.0

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 +121 -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 +420 -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 +419 -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 +822 -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
package/src/hotkey.ts ADDED
@@ -0,0 +1,411 @@
1
+ /**
2
+ * All supported modifier key names, including aliases.
3
+ * - Control/Ctrl: The Control key
4
+ * - Shift: The Shift key
5
+ * - Alt/Option: The Alt key (Option on macOS)
6
+ * - Command/Cmd: The Command key (macOS only)
7
+ * - CommandOrControl/Mod: Command on macOS, Control on other platforms
8
+ */
9
+ export type Modifier =
10
+ | 'Control'
11
+ | 'Ctrl'
12
+ | 'Shift'
13
+ | 'Alt'
14
+ | 'Option'
15
+ | 'Command'
16
+ | 'Cmd'
17
+ | 'CommandOrControl'
18
+ | 'Mod'
19
+
20
+ /**
21
+ * Canonical modifier names that map to KeyboardEvent properties.
22
+ */
23
+ export type CanonicalModifier = 'Control' | 'Shift' | 'Alt' | 'Meta'
24
+
25
+ /**
26
+ * Letter keys A-Z (case-insensitive in matching).
27
+ */
28
+ export type LetterKey =
29
+ | 'A'
30
+ | 'B'
31
+ | 'C'
32
+ | 'D'
33
+ | 'E'
34
+ | 'F'
35
+ | 'G'
36
+ | 'H'
37
+ | 'I'
38
+ | 'J'
39
+ | 'K'
40
+ | 'L'
41
+ | 'M'
42
+ | 'N'
43
+ | 'O'
44
+ | 'P'
45
+ | 'Q'
46
+ | 'R'
47
+ | 'S'
48
+ | 'T'
49
+ | 'U'
50
+ | 'V'
51
+ | 'W'
52
+ | 'X'
53
+ | 'Y'
54
+ | 'Z'
55
+
56
+ /**
57
+ * Number keys 0-9.
58
+ */
59
+ export type NumberKey =
60
+ | '0'
61
+ | '1'
62
+ | '2'
63
+ | '3'
64
+ | '4'
65
+ | '5'
66
+ | '6'
67
+ | '7'
68
+ | '8'
69
+ | '9'
70
+
71
+ /**
72
+ * Function keys F1-F12.
73
+ */
74
+ export type FunctionKey =
75
+ | 'F1'
76
+ | 'F2'
77
+ | 'F3'
78
+ | 'F4'
79
+ | 'F5'
80
+ | 'F6'
81
+ | 'F7'
82
+ | 'F8'
83
+ | 'F9'
84
+ | 'F10'
85
+ | 'F11'
86
+ | 'F12'
87
+
88
+ /**
89
+ * Navigation keys for cursor movement.
90
+ */
91
+ export type NavigationKey =
92
+ | 'ArrowUp'
93
+ | 'ArrowDown'
94
+ | 'ArrowLeft'
95
+ | 'ArrowRight'
96
+ | 'Home'
97
+ | 'End'
98
+ | 'PageUp'
99
+ | 'PageDown'
100
+
101
+ /**
102
+ * Editing and special keys.
103
+ */
104
+ export type EditingKey =
105
+ | 'Enter'
106
+ | 'Escape'
107
+ | 'Space'
108
+ | 'Tab'
109
+ | 'Backspace'
110
+ | 'Delete'
111
+
112
+ /**
113
+ * Punctuation keys commonly used in keyboard shortcuts.
114
+ * These are the literal characters as they appear in KeyboardEvent.key
115
+ * (layout-dependent, typically US keyboard layout).
116
+ */
117
+ export type PunctuationKey =
118
+ | '/'
119
+ | '['
120
+ | ']'
121
+ | '\\'
122
+ | '='
123
+ | '-'
124
+ | ','
125
+ | '.'
126
+ | '`'
127
+
128
+ /**
129
+ * Keys that don't change their value when Shift is pressed.
130
+ * These keys produce the same `KeyboardEvent.key` value whether Shift is held or not.
131
+ *
132
+ * Excludes NumberKey (Shift+1 produces '!' on US layout) and PunctuationKey
133
+ * (Shift+',' produces '<' on US layout).
134
+ *
135
+ * Used in hotkey type definitions to prevent layout-dependent issues when Shift
136
+ * is part of the modifier combination.
137
+ */
138
+ type NonPunctuationKey =
139
+ | LetterKey
140
+ | NumberKey
141
+ | EditingKey
142
+ | NavigationKey
143
+ | FunctionKey
144
+
145
+ /**
146
+ * All supported non-modifier keys.
147
+ */
148
+ export type Key = NonPunctuationKey | PunctuationKey
149
+
150
+ /**
151
+ * Keys that can be tracked as "held" (pressed down).
152
+ * Includes both modifier keys and regular keys.
153
+ */
154
+ export type HeldKey = CanonicalModifier | Key
155
+
156
+ // =============================================================================
157
+ // Hotkey Types
158
+ // =============================================================================
159
+
160
+ /**
161
+ * Single modifier + key combinations.
162
+ * Uses canonical modifiers (4) + Mod (1) = 5 modifiers.
163
+ * Shift combinations exclude PunctuationKey to avoid layout-dependent issues.
164
+ *
165
+ * The `Mod` modifier is platform-adaptive:
166
+ * - **macOS**: Resolves to `Meta` (Command key ⌘)
167
+ * - **Windows/Linux**: Resolves to `Control` (Ctrl key)
168
+ *
169
+ * This enables cross-platform hotkey definitions that automatically adapt to the platform.
170
+ * For example, `Mod+S` becomes `Command+S` on Mac and `Ctrl+S` on Windows/Linux.
171
+ */
172
+ type SingleModifierHotkey =
173
+ | `Control+${Key}`
174
+ | `Alt+${Key}`
175
+ | `Shift+${NonPunctuationKey}`
176
+ | `Meta+${Key}`
177
+ | `Mod+${Key}`
178
+
179
+ /**
180
+ * Two modifier + key combinations.
181
+ * Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
182
+ *
183
+ * **Platform-adaptive `Mod` combinations:**
184
+ * - `Mod+Alt` and `Mod+Shift` are included (safe on all platforms)
185
+ * - `Mod+Control` and `Mod+Meta` are excluded because they create duplicate modifiers:
186
+ * - `Mod+Control` duplicates `Control` on Windows/Linux (Mod = Control)
187
+ * - `Mod+Meta` duplicates `Meta` on macOS (Mod = Meta)
188
+ */
189
+ type TwoModifierHotkey =
190
+ | `Control+Alt+${Key}`
191
+ | `Control+Shift+${NonPunctuationKey}`
192
+ | `Control+Meta+${Key}`
193
+ | `Alt+Shift+${NonPunctuationKey}`
194
+ | `Alt+Meta+${Key}`
195
+ | `Shift+Meta+${NonPunctuationKey}`
196
+ | `Mod+Alt+${Key}`
197
+ | `Mod+Shift+${NonPunctuationKey}`
198
+
199
+ /**
200
+ * Three modifier + key combinations.
201
+ * Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
202
+ *
203
+ * **Platform-adaptive `Mod` combinations:**
204
+ * - `Mod+Alt+Shift` is included (safe on all platforms)
205
+ * - `Mod+Control+Shift` and `Mod+Shift+Meta` are excluded because they create duplicate modifiers:
206
+ * - `Mod+Control+Shift` duplicates `Control` on Windows/Linux (Mod = Control)
207
+ * - `Mod+Shift+Meta` duplicates `Meta` on macOS (Mod = Meta)
208
+ */
209
+ type ThreeModifierHotkey =
210
+ | `Control+Alt+Shift+${NonPunctuationKey}`
211
+ | `Control+Alt+Meta+${Key}`
212
+ | `Control+Shift+Meta+${NonPunctuationKey}`
213
+ | `Alt+Shift+Meta+${NonPunctuationKey}`
214
+ | `Mod+Alt+Shift+${NonPunctuationKey}`
215
+
216
+ /**
217
+ * Four modifier + key combinations.
218
+ * Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
219
+ *
220
+ * Only the canonical `Control+Alt+Shift+Meta` combination is included.
221
+ *
222
+ * **Why no `Mod` combinations?**
223
+ * Since `Mod` resolves to either `Control` (Windows/Linux) or `Meta` (macOS), any
224
+ * four-modifier combination with `Mod` would create duplicate modifiers on one platform.
225
+ * For example:
226
+ * - `Mod+Control+Alt+Shift` → duplicates `Control` on Windows/Linux
227
+ * - `Mod+Alt+Shift+Meta` → duplicates `Meta` on macOS
228
+ */
229
+ type FourModifierHotkey = `Control+Alt+Shift+Meta+${NonPunctuationKey}`
230
+
231
+ /**
232
+ * A type-safe hotkey string.
233
+ *
234
+ * Provides autocomplete for:
235
+ * - All single keys (letters, numbers, function keys, navigation, editing, punctuation)
236
+ * - Single modifier + common key (Control+S, Mod+A, Mod+/, etc.)
237
+ * - Two modifiers + common key (Mod+Shift+S, Control+Alt+A, etc.)
238
+ * - Three modifiers + common key (Control+Alt+Shift+A, Mod+Alt+Shift+S, etc.)
239
+ * - Four modifiers + common key (Control+Alt+Shift+Meta+A, etc.)
240
+ *
241
+ * ## Modifier Names
242
+ *
243
+ * Use canonical modifier names:
244
+ * - `Control` (not Ctrl) - The Control key
245
+ * - `Alt` (not Option) - The Alt key (Option on macOS)
246
+ * - `Meta` (not Command/Cmd) - The Meta/Command key (macOS only)
247
+ * - `Shift` - The Shift key
248
+ *
249
+ * ## Platform-Adaptive `Mod` Modifier
250
+ *
251
+ * The `Mod` modifier is a special platform-adaptive modifier that automatically resolves
252
+ * to the "primary modifier" on each platform:
253
+ *
254
+ * - **macOS**: `Mod` → `Meta` (Command key ⌘)
255
+ * - **Windows/Linux**: `Mod` → `Control` (Ctrl key)
256
+ *
257
+ * This enables cross-platform hotkey definitions that work correctly on all platforms
258
+ * without platform-specific code. The `Mod` modifier is resolved at runtime based on
259
+ * the detected platform.
260
+ *
261
+ * **When to use `Mod` vs platform-specific modifiers:**
262
+ * - Use `Mod` for cross-platform shortcuts (e.g., `Mod+S` for save)
263
+ * - Use `Meta` or `Control` when you need platform-specific behavior
264
+ * - Use `Mod` when you want your shortcuts to follow platform conventions automatically
265
+ *
266
+ * **Limitations:**
267
+ * - `Mod+Control` and `Mod+Meta` combinations are not allowed (they create duplicate
268
+ * modifiers on one platform)
269
+ * - In four-modifier combinations, only canonical modifiers are allowed (no `Mod`)
270
+ *
271
+ * @example
272
+ * ```ts
273
+ * // Cross-platform shortcuts (recommended)
274
+ * const save: Hotkey = 'Mod+S' // Command+S on Mac, Ctrl+S on Windows/Linux
275
+ * const saveAs: Hotkey = 'Mod+Shift+S' // Command+Shift+S on Mac, Ctrl+Shift+S elsewhere
276
+ * const comment: Hotkey = 'Mod+/' // Command+/ on Mac, Ctrl+/ elsewhere
277
+ *
278
+ * // Platform-specific shortcuts
279
+ * const macOnly: Hotkey = 'Meta+S' // Command+S on Mac only
280
+ * const windowsOnly: Hotkey = 'Control+S' // Ctrl+S on Windows/Linux only
281
+ * ```
282
+ */
283
+ export type Hotkey =
284
+ | Key
285
+ | SingleModifierHotkey
286
+ | TwoModifierHotkey
287
+ | ThreeModifierHotkey
288
+ | FourModifierHotkey
289
+
290
+ /**
291
+ * A parsed representation of a hotkey string.
292
+ *
293
+ * This interface provides a flexible fallback when the `Hotkey` type doesn't
294
+ * fit your use case. You can pass a `ParsedHotkey` directly to hotkey functions
295
+ * instead of a hotkey string, allowing for more dynamic or complex scenarios
296
+ * that aren't covered by the type-safe `Hotkey` union.
297
+ *
298
+ * @example
299
+ * ```ts
300
+ * // Type-safe hotkey string
301
+ * useHotkey('Mod+S', handler)
302
+ *
303
+ * // Fallback: parsed hotkey for dynamic scenarios
304
+ * const parsed = parseHotkey(userInput)
305
+ * useHotkey(parsed, handler) // Works even if userInput isn't in Hotkey type
306
+ * ```
307
+ */
308
+ export interface ParsedHotkey {
309
+ /** The non-modifier key (e.g., 'S', 'Escape', 'F1', '/', '['). Can be any string for flexibility. */
310
+ key: Key | (string & {})
311
+ /** Whether the Control key is required */
312
+ ctrl: boolean
313
+ /** Whether the Shift key is required */
314
+ shift: boolean
315
+ /** Whether the Alt key is required */
316
+ alt: boolean
317
+ /** Whether the Meta (Command) key is required */
318
+ meta: boolean
319
+ /** List of canonical modifier names that are required, in canonical order */
320
+ modifiers: Array<CanonicalModifier>
321
+ }
322
+
323
+ /**
324
+ * A raw hotkey object for programmatic registration.
325
+ *
326
+ * Like `ParsedHotkey` but without `modifiers` (derived from booleans)
327
+ * and with optional modifier booleans (default to `false` when omitted).
328
+ * Use with `HotkeyManager.register()` and `useHotkey()` when you prefer
329
+ * object form over a string.
330
+ *
331
+ * The `mod` modifier is platform-adaptive: Command on macOS, Control on Windows/Linux.
332
+ * Pass `platform` when converting to ParsedHotkey (e.g., via `options.platform`).
333
+ *
334
+ * @example
335
+ * ```ts
336
+ * useHotkey({ key: 'S', mod: true }, handler) // Mod+S (cross-platform)
337
+ * useHotkey({ key: 'S', ctrl: true }, handler) // Control+S
338
+ * useHotkey({ key: 'Escape' }, handler) // Escape (no modifiers)
339
+ * useHotkey({ key: 'A', shift: true, meta: true }, handler) // Shift+Meta+A
340
+ * useHotkey({ key: 'S', mod: true, shift: true }, handler) // Mod+Shift+S
341
+ * ```
342
+ */
343
+ export interface RawHotkey {
344
+ /** The non-modifier key (e.g., 'S', 'Escape', 'F1'). */
345
+ key: Key | (string & {})
346
+ /** Platform-adaptive modifier: Command on macOS, Control on Windows/Linux. Defaults to false. */
347
+ mod?: boolean
348
+ /** Whether the Control key is required. Defaults to false. */
349
+ ctrl?: boolean
350
+ /** Whether the Shift key is required. Defaults to false. */
351
+ shift?: boolean
352
+ /** Whether the Alt key is required. Defaults to false. */
353
+ alt?: boolean
354
+ /** Whether the Meta (Command) key is required. Defaults to false. */
355
+ meta?: boolean
356
+ }
357
+
358
+ /**
359
+ * A hotkey that can be passed to `HotkeyManager.register()` and `useHotkey()`.
360
+ * Either a type-safe string (`Hotkey`) or a raw object (`RawHotkey`).
361
+ */
362
+ export type RegisterableHotkey = Hotkey | RawHotkey
363
+
364
+ /**
365
+ * Options for formatting hotkeys for display.
366
+ */
367
+ export interface FormatDisplayOptions {
368
+ /** The target platform. Defaults to auto-detection. */
369
+ platform?: 'mac' | 'windows' | 'linux'
370
+ }
371
+
372
+ /**
373
+ * Result of validating a hotkey string.
374
+ */
375
+ export interface ValidationResult {
376
+ /** Whether the hotkey is valid (can still have warnings) */
377
+ valid: boolean
378
+ /** Warning messages about potential issues */
379
+ warnings: Array<string>
380
+ /** Error messages about invalid syntax */
381
+ errors: Array<string>
382
+ }
383
+
384
+ /**
385
+ * Context passed to hotkey callbacks along with the keyboard event.
386
+ */
387
+ export interface HotkeyCallbackContext {
388
+ /** The original hotkey string that was registered */
389
+ hotkey: Hotkey
390
+ /** The parsed representation of the hotkey */
391
+ parsedHotkey: ParsedHotkey
392
+ }
393
+
394
+ /**
395
+ * Callback function type for hotkey handlers.
396
+ *
397
+ * @param event - The keyboard event that triggered the hotkey
398
+ * @param context - Additional context including the hotkey and parsed hotkey
399
+ *
400
+ * @example
401
+ * ```ts
402
+ * const handler: HotkeyCallback = (event, { hotkey, parsedHotkey }) => {
403
+ * console.log(`Hotkey ${hotkey} was pressed`)
404
+ * console.log(`Modifiers:`, parsedHotkey.modifiers)
405
+ * }
406
+ * ```
407
+ */
408
+ export type HotkeyCallback = (
409
+ event: KeyboardEvent,
410
+ context: HotkeyCallbackContext,
411
+ ) => void
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from './constants'
2
+ export * from './format'
3
+ export * from './hotkey'
4
+ export * from './hotkey-manager'
5
+ export * from './key-state-tracker'
6
+ export * from './match'
7
+ export * from './parse'
8
+ export * from './recorder'
9
+ export * from './sequence'
10
+ export * from './validate'
@@ -0,0 +1,249 @@
1
+ import { Store } from '@tanstack/store'
2
+ import { MODIFIER_KEYS, normalizeKeyName } from './constants'
3
+
4
+ /**
5
+ * State interface for the KeyStateTracker.
6
+ */
7
+ export interface KeyStateTrackerState {
8
+ /**
9
+ * Array of currently held key names (normalized, e.g. "Control", "A").
10
+ */
11
+ heldKeys: Array<string>
12
+ /**
13
+ * Map from normalized key name to the physical `event.code` (e.g. "KeyA", "ShiftLeft").
14
+ * Useful for debugging which physical key was pressed.
15
+ */
16
+ heldCodes: Record<string, string>
17
+ }
18
+
19
+ /**
20
+ * Returns the default state for KeyStateTracker.
21
+ */
22
+ function getDefaultKeyStateTrackerState(): KeyStateTrackerState {
23
+ return {
24
+ heldKeys: [],
25
+ heldCodes: {},
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Singleton tracker for currently held keyboard keys.
31
+ *
32
+ * This class maintains a list of all keys currently being pressed,
33
+ * which is useful for:
34
+ * - Displaying currently held keys to users
35
+ * - Custom shortcut recording for rebinding
36
+ * - Complex chord detection
37
+ *
38
+ * State Management:
39
+ * - Uses TanStack Store for reactive state management
40
+ * - State can be accessed via `tracker.store.state` when using the class directly
41
+ * - When using framework adapters (React), use `useHeldKeys` and `useHeldKeyCodes` hooks for reactive state
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const tracker = KeyStateTracker.getInstance()
46
+ *
47
+ * // Access state directly
48
+ * console.log(tracker.store.state.heldKeys) // ['Control', 'Shift']
49
+ *
50
+ * // Subscribe to changes with TanStack Store
51
+ * const unsubscribe = tracker.store.subscribe(() => {
52
+ * console.log('Currently held:', tracker.store.state.heldKeys)
53
+ * })
54
+ *
55
+ * // Check current state
56
+ * console.log(tracker.getHeldKeys()) // ['Control', 'Shift']
57
+ * console.log(tracker.isKeyHeld('Control')) // true
58
+ *
59
+ * // Cleanup
60
+ * unsubscribe()
61
+ * ```
62
+ */
63
+ export class KeyStateTracker {
64
+ static #instance: KeyStateTracker | null = null
65
+
66
+ /**
67
+ * The TanStack Store instance containing the tracker state.
68
+ * Use this to subscribe to state changes or access current state.
69
+ */
70
+ readonly store: Store<KeyStateTrackerState> = new Store(
71
+ getDefaultKeyStateTrackerState(),
72
+ )
73
+
74
+ #heldKeysSet: Set<string> = new Set()
75
+ #heldCodesMap: Map<string, string> = new Map()
76
+ #keydownListener: ((event: KeyboardEvent) => void) | null = null
77
+ #keyupListener: ((event: KeyboardEvent) => void) | null = null
78
+ #blurListener: (() => void) | null = null
79
+
80
+ private constructor() {
81
+ this.#setupListeners()
82
+ }
83
+
84
+ /**
85
+ * Gets the singleton instance of KeyStateTracker.
86
+ */
87
+ static getInstance(): KeyStateTracker {
88
+ if (!KeyStateTracker.#instance) {
89
+ KeyStateTracker.#instance = new KeyStateTracker()
90
+ }
91
+ return KeyStateTracker.#instance
92
+ }
93
+
94
+ /**
95
+ * Resets the singleton instance. Useful for testing.
96
+ */
97
+ static resetInstance(): void {
98
+ if (KeyStateTracker.#instance) {
99
+ KeyStateTracker.#instance.destroy()
100
+ KeyStateTracker.#instance = null
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Sets up the keyboard event listeners.
106
+ */
107
+ #setupListeners(): void {
108
+ if (typeof document === 'undefined') {
109
+ return // SSR safety
110
+ }
111
+
112
+ this.#keydownListener = (event: KeyboardEvent) => {
113
+ const key = normalizeKeyName(event.key)
114
+ if (!this.#heldKeysSet.has(key)) {
115
+ this.#heldKeysSet.add(key)
116
+ this.#heldCodesMap.set(key, event.code)
117
+ this.#syncState()
118
+ }
119
+ }
120
+
121
+ this.#keyupListener = (event: KeyboardEvent) => {
122
+ const key = normalizeKeyName(event.key)
123
+ if (this.#heldKeysSet.has(key)) {
124
+ this.#heldKeysSet.delete(key)
125
+ this.#heldCodesMap.delete(key)
126
+ }
127
+
128
+ // When a modifier key is released, clear any non-modifier keys still
129
+ // marked as held. On macOS, the OS intercepts modifier+key combos
130
+ // (e.g. Cmd+S) and swallows the keyup event for the non-modifier key,
131
+ // leaving it permanently stuck in the held set.
132
+ if (MODIFIER_KEYS.has(key)) {
133
+ for (const heldKey of this.#heldKeysSet) {
134
+ if (!MODIFIER_KEYS.has(heldKey)) {
135
+ this.#heldKeysSet.delete(heldKey)
136
+ this.#heldCodesMap.delete(heldKey)
137
+ }
138
+ }
139
+ }
140
+
141
+ this.#syncState()
142
+ }
143
+
144
+ // Clear all keys when window loses focus (keys might be released while not focused)
145
+ this.#blurListener = () => {
146
+ if (this.#heldKeysSet.size > 0) {
147
+ this.#heldKeysSet.clear()
148
+ this.#heldCodesMap.clear()
149
+ this.#syncState()
150
+ }
151
+ }
152
+
153
+ document.addEventListener('keydown', this.#keydownListener)
154
+ document.addEventListener('keyup', this.#keyupListener)
155
+ window.addEventListener('blur', this.#blurListener)
156
+ }
157
+
158
+ /**
159
+ * Syncs the internal Set to the Store state.
160
+ */
161
+ #syncState(): void {
162
+ this.store.setState(() => ({
163
+ heldKeys: Array.from(this.#heldKeysSet),
164
+ heldCodes: Object.fromEntries(this.#heldCodesMap),
165
+ }))
166
+ }
167
+
168
+ /**
169
+ * Removes the keyboard event listeners.
170
+ */
171
+ #removeListeners(): void {
172
+ if (typeof document === 'undefined') {
173
+ return
174
+ }
175
+
176
+ if (this.#keydownListener) {
177
+ document.removeEventListener('keydown', this.#keydownListener)
178
+ this.#keydownListener = null
179
+ }
180
+
181
+ if (this.#keyupListener) {
182
+ document.removeEventListener('keyup', this.#keyupListener)
183
+ this.#keyupListener = null
184
+ }
185
+
186
+ if (this.#blurListener) {
187
+ window.removeEventListener('blur', this.#blurListener)
188
+ this.#blurListener = null
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Gets an array of currently held key names.
194
+ *
195
+ * @returns Array of key names currently being pressed
196
+ */
197
+ getHeldKeys(): Array<string> {
198
+ return this.store.state.heldKeys
199
+ }
200
+
201
+ /**
202
+ * Checks if a specific key is currently being held.
203
+ *
204
+ * @param key - The key name to check (case-insensitive)
205
+ * @returns True if the key is currently held
206
+ */
207
+ isKeyHeld(key: string): boolean {
208
+ const normalizedKey = normalizeKeyName(key)
209
+ return this.#heldKeysSet.has(normalizedKey)
210
+ }
211
+
212
+ /**
213
+ * Checks if any of the given keys are currently held.
214
+ *
215
+ * @param keys - Array of key names to check
216
+ * @returns True if any of the keys are currently held
217
+ */
218
+ isAnyKeyHeld(keys: Array<string>): boolean {
219
+ return keys.some((key) => this.isKeyHeld(key))
220
+ }
221
+
222
+ /**
223
+ * Checks if all of the given keys are currently held.
224
+ *
225
+ * @param keys - Array of key names to check
226
+ * @returns True if all of the keys are currently held
227
+ */
228
+ areAllKeysHeld(keys: Array<string>): boolean {
229
+ return keys.every((key) => this.isKeyHeld(key))
230
+ }
231
+
232
+ /**
233
+ * Destroys the tracker and removes all listeners.
234
+ */
235
+ destroy(): void {
236
+ this.#removeListeners()
237
+ this.#heldKeysSet.clear()
238
+ this.#heldCodesMap.clear()
239
+ this.store.setState(() => getDefaultKeyStateTrackerState())
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Gets the singleton KeyStateTracker instance.
245
+ * Convenience function for accessing the tracker.
246
+ */
247
+ export function getKeyStateTracker(): KeyStateTracker {
248
+ return KeyStateTracker.getInstance()
249
+ }