@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,798 @@
1
+ import { Store } from '@tanstack/store'
2
+ import { detectPlatform, normalizeKeyName } from './constants'
3
+ import { formatHotkey } from './format'
4
+ import { parseHotkey, rawHotkeyToParsedHotkey } from './parse'
5
+ import { matchesKeyboardEvent } from './match'
6
+ import type {
7
+ Hotkey,
8
+ HotkeyCallback,
9
+ HotkeyCallbackContext,
10
+ ParsedHotkey,
11
+ RegisterableHotkey,
12
+ } from './hotkey'
13
+
14
+ /**
15
+ * Behavior when registering a hotkey that conflicts with an existing registration.
16
+ *
17
+ * - `'warn'` - Log a warning to the console but allow both registrations (default)
18
+ * - `'error'` - Throw an error and prevent the new registration
19
+ * - `'replace'` - Unregister the existing hotkey and register the new one
20
+ * - `'allow'` - Allow multiple registrations of the same hotkey without warning
21
+ */
22
+ export type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow'
23
+
24
+ /**
25
+ * Options for registering a hotkey.
26
+ */
27
+ export interface HotkeyOptions {
28
+ /** Behavior when this hotkey conflicts with an existing registration on the same target. Defaults to 'warn' */
29
+ conflictBehavior?: ConflictBehavior
30
+ /** Whether the hotkey is enabled. Defaults to true */
31
+ enabled?: boolean
32
+ /** The event type to listen for. Defaults to 'keydown' */
33
+ eventType?: 'keydown' | 'keyup'
34
+ /** Whether to ignore hotkeys when keyboard events originate from input-like elements (input, textarea, select, contenteditable). Defaults to true */
35
+ ignoreInputs?: boolean
36
+ /** The target platform for resolving 'Mod' */
37
+ platform?: 'mac' | 'windows' | 'linux'
38
+ /** Prevent the default browser action when the hotkey matches. Defaults to true */
39
+ preventDefault?: boolean
40
+ /** If true, only trigger once until all keys are released. Default: false */
41
+ requireReset?: boolean
42
+ /** Stop event propagation when the hotkey matches. Defaults to true */
43
+ stopPropagation?: boolean
44
+ /** The DOM element to attach the event listener to. Defaults to document. */
45
+ target?: HTMLElement | Document | Window | null
46
+ }
47
+
48
+ /**
49
+ * A registered hotkey handler in the HotkeyManager.
50
+ */
51
+ export interface HotkeyRegistration {
52
+ /** The callback to invoke */
53
+ callback: HotkeyCallback
54
+ /** Whether this registration has fired and needs reset (for requireReset) */
55
+ hasFired: boolean
56
+ /** The original hotkey string */
57
+ hotkey: Hotkey
58
+ /** Unique identifier for this registration */
59
+ id: string
60
+ /** Options for this registration */
61
+ options: HotkeyOptions
62
+ /** The parsed hotkey */
63
+ parsedHotkey: ParsedHotkey
64
+ /** The resolved target element for this registration */
65
+ target: HTMLElement | Document | Window
66
+ /** How many times this registration's callback has been triggered */
67
+ triggerCount: number
68
+ }
69
+
70
+ /**
71
+ * A handle returned from HotkeyManager.register() that allows updating
72
+ * the callback and options without re-registering the hotkey.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const handle = manager.register('Mod+S', callback, options)
77
+ *
78
+ * // Update callback without re-registering (avoids stale closures)
79
+ * handle.callback = newCallback
80
+ *
81
+ * // Update options without re-registering
82
+ * handle.setOptions({ enabled: false })
83
+ *
84
+ * // Check if still active
85
+ * if (handle.isActive) {
86
+ * // ...
87
+ * }
88
+ *
89
+ * // Unregister when done
90
+ * handle.unregister()
91
+ * ```
92
+ */
93
+ export interface HotkeyRegistrationHandle {
94
+ /**
95
+ * The callback function. Can be set directly to update without re-registering.
96
+ * This avoids stale closures when the callback references React state.
97
+ */
98
+ callback: HotkeyCallback
99
+ /** Unique identifier for this registration */
100
+ readonly id: string
101
+ /** Check if this registration is still active (not unregistered) */
102
+ readonly isActive: boolean
103
+ /**
104
+ * Update options (merged with existing options).
105
+ * Useful for updating `enabled`, `preventDefault`, etc. without re-registering.
106
+ */
107
+ setOptions: (options: Partial<HotkeyOptions>) => void
108
+ /** Unregister this hotkey */
109
+ unregister: () => void
110
+ }
111
+
112
+ /**
113
+ * Default options for hotkey registration.
114
+ */
115
+ const defaultHotkeyOptions: Omit<
116
+ Required<HotkeyOptions>,
117
+ 'platform' | 'target'
118
+ > = {
119
+ preventDefault: true,
120
+ stopPropagation: true,
121
+ eventType: 'keydown',
122
+ requireReset: false,
123
+ enabled: true,
124
+ ignoreInputs: true,
125
+ conflictBehavior: 'warn',
126
+ }
127
+
128
+ let registrationIdCounter = 0
129
+
130
+ /**
131
+ * Generates a unique ID for hotkey registrations.
132
+ */
133
+ function generateId(): string {
134
+ return `hotkey_${++registrationIdCounter}`
135
+ }
136
+
137
+ /**
138
+ * Singleton manager for hotkey registrations.
139
+ *
140
+ * This class provides a centralized way to register and manage keyboard hotkeys.
141
+ * It uses a single event listener for efficiency, regardless of how many hotkeys
142
+ * are registered.
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * const manager = HotkeyManager.getInstance()
147
+ *
148
+ * const unregister = manager.register('Mod+S', (event, context) => {
149
+ * console.log('Save triggered!')
150
+ * })
151
+ *
152
+ * // Later, to unregister:
153
+ * unregister()
154
+ * ```
155
+ */
156
+ export class HotkeyManager {
157
+ static #instance: HotkeyManager | null = null
158
+
159
+ /**
160
+ * The TanStack Store containing all hotkey registrations.
161
+ * Use this to subscribe to registration changes or access current registrations.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * const manager = HotkeyManager.getInstance()
166
+ *
167
+ * // Subscribe to registration changes
168
+ * const unsubscribe = manager.registrations.subscribe(() => {
169
+ * console.log('Registrations changed:', manager.registrations.state.size)
170
+ * })
171
+ *
172
+ * // Access current registrations
173
+ * for (const [id, reg] of manager.registrations.state) {
174
+ * console.log(reg.hotkey, reg.options.enabled)
175
+ * }
176
+ * ```
177
+ */
178
+ readonly registrations: Store<Map<string, HotkeyRegistration>> = new Store(
179
+ new Map(),
180
+ )
181
+ #platform: 'mac' | 'windows' | 'linux'
182
+ #targetListeners: Map<
183
+ HTMLElement | Document | Window,
184
+ {
185
+ keydown: (event: KeyboardEvent) => void
186
+ keyup: (event: KeyboardEvent) => void
187
+ }
188
+ > = new Map()
189
+ #targetRegistrations: Map<HTMLElement | Document | Window, Set<string>> =
190
+ new Map()
191
+
192
+ private constructor() {
193
+ this.#platform = detectPlatform()
194
+ }
195
+
196
+ /**
197
+ * Gets the singleton instance of HotkeyManager.
198
+ */
199
+ static getInstance(): HotkeyManager {
200
+ if (!HotkeyManager.#instance) {
201
+ HotkeyManager.#instance = new HotkeyManager()
202
+ }
203
+ return HotkeyManager.#instance
204
+ }
205
+
206
+ /**
207
+ * Resets the singleton instance. Useful for testing.
208
+ */
209
+ static resetInstance(): void {
210
+ if (HotkeyManager.#instance) {
211
+ HotkeyManager.#instance.destroy()
212
+ HotkeyManager.#instance = null
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Registers a hotkey handler and returns a handle for updating the registration.
218
+ *
219
+ * The returned handle allows updating the callback and options without
220
+ * re-registering, which is useful for avoiding stale closures in React.
221
+ *
222
+ * @param hotkey - The hotkey string (e.g., 'Mod+S') or RawHotkey object
223
+ * @param callback - The function to call when the hotkey is pressed
224
+ * @param options - Options for the hotkey behavior
225
+ * @returns A handle for managing the registration
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * const handle = manager.register('Mod+S', callback)
230
+ *
231
+ * // Update callback without re-registering (avoids stale closures)
232
+ * handle.callback = newCallback
233
+ *
234
+ * // Update options
235
+ * handle.setOptions({ enabled: false })
236
+ *
237
+ * // Unregister when done
238
+ * handle.unregister()
239
+ * ```
240
+ */
241
+ register(
242
+ hotkey: RegisterableHotkey,
243
+ callback: HotkeyCallback,
244
+ options: HotkeyOptions = {},
245
+ ): HotkeyRegistrationHandle {
246
+ const id = generateId()
247
+ const platform = options.platform ?? this.#platform
248
+ const parsedHotkey =
249
+ typeof hotkey === 'string'
250
+ ? parseHotkey(hotkey, platform)
251
+ : rawHotkeyToParsedHotkey(hotkey, platform)
252
+ const hotkeyStr = (
253
+ typeof hotkey === 'string' ? hotkey : formatHotkey(parsedHotkey)
254
+ ) as Hotkey
255
+
256
+ // Resolve target: default to document if not provided or null
257
+ const target =
258
+ options.target ??
259
+ (typeof document !== 'undefined' ? document : ({} as Document))
260
+
261
+ // Resolve conflict behavior
262
+ const conflictBehavior = options.conflictBehavior ?? 'warn'
263
+
264
+ // Check for existing registrations with the same hotkey and target
265
+ const conflictingRegistration = this.#findConflictingRegistration(
266
+ hotkeyStr,
267
+ target,
268
+ )
269
+
270
+ if (conflictingRegistration) {
271
+ this.#handleConflict(conflictingRegistration, hotkeyStr, conflictBehavior)
272
+ }
273
+
274
+ const registration: HotkeyRegistration = {
275
+ id,
276
+ hotkey: hotkeyStr,
277
+ parsedHotkey,
278
+ callback,
279
+ options: {
280
+ ...defaultHotkeyOptions,
281
+ ...options,
282
+ platform,
283
+ },
284
+ hasFired: false,
285
+ triggerCount: 0,
286
+ target,
287
+ }
288
+
289
+ this.registrations.setState((prev) => new Map(prev).set(id, registration))
290
+
291
+ // Track registration for this target
292
+ if (!this.#targetRegistrations.has(target)) {
293
+ this.#targetRegistrations.set(target, new Set())
294
+ }
295
+ this.#targetRegistrations.get(target)!.add(id)
296
+
297
+ // Ensure listeners are attached for this target
298
+ this.#ensureListenersForTarget(target)
299
+
300
+ // Create and return the handle
301
+ const manager = this
302
+ const handle: HotkeyRegistrationHandle = {
303
+ get id() {
304
+ return id
305
+ },
306
+ unregister: () => {
307
+ manager.#unregister(id)
308
+ },
309
+ get callback() {
310
+ const reg = manager.registrations.state.get(id)
311
+ return reg?.callback ?? callback
312
+ },
313
+ set callback(newCallback: HotkeyCallback) {
314
+ const reg = manager.registrations.state.get(id)
315
+ if (reg) {
316
+ reg.callback = newCallback
317
+ }
318
+ },
319
+ setOptions: (newOptions: Partial<HotkeyOptions>) => {
320
+ manager.registrations.setState((prev) => {
321
+ const reg = prev.get(id)
322
+ if (reg) {
323
+ const next = new Map(prev)
324
+ next.set(id, { ...reg, options: { ...reg.options, ...newOptions } })
325
+ return next
326
+ }
327
+ return prev
328
+ })
329
+ },
330
+ get isActive() {
331
+ return manager.registrations.state.has(id)
332
+ },
333
+ }
334
+
335
+ return handle
336
+ }
337
+
338
+ /**
339
+ * Unregisters a hotkey by its registration ID.
340
+ */
341
+ #unregister(id: string): void {
342
+ const registration = this.registrations.state.get(id)
343
+ if (!registration) {
344
+ return
345
+ }
346
+
347
+ const target = registration.target
348
+
349
+ // Remove registration
350
+ this.registrations.setState((prev) => {
351
+ const next = new Map(prev)
352
+ next.delete(id)
353
+ return next
354
+ })
355
+
356
+ // Remove from target registrations tracking
357
+ const targetRegs = this.#targetRegistrations.get(target)
358
+ if (targetRegs) {
359
+ targetRegs.delete(id)
360
+ // If no more registrations for this target, remove listeners
361
+ if (targetRegs.size === 0) {
362
+ this.#removeListenersForTarget(target)
363
+ }
364
+ }
365
+ }
366
+
367
+ /**
368
+ * Ensures event listeners are attached for a specific target.
369
+ */
370
+ #ensureListenersForTarget(target: HTMLElement | Document | Window): void {
371
+ if (typeof document === 'undefined') {
372
+ return // SSR safety
373
+ }
374
+
375
+ // Skip if listeners already exist for this target
376
+ if (this.#targetListeners.has(target)) {
377
+ return
378
+ }
379
+
380
+ const keydownHandler = this.#createTargetKeyDownHandler(target)
381
+ const keyupHandler = this.#createTargetKeyUpHandler(target)
382
+
383
+ target.addEventListener('keydown', keydownHandler as EventListener)
384
+ target.addEventListener('keyup', keyupHandler as EventListener)
385
+
386
+ this.#targetListeners.set(target, {
387
+ keydown: keydownHandler,
388
+ keyup: keyupHandler,
389
+ })
390
+ }
391
+
392
+ /**
393
+ * Removes event listeners for a specific target.
394
+ */
395
+ #removeListenersForTarget(target: HTMLElement | Document | Window): void {
396
+ if (typeof document === 'undefined') {
397
+ return
398
+ }
399
+
400
+ const listeners = this.#targetListeners.get(target)
401
+ if (!listeners) {
402
+ return
403
+ }
404
+
405
+ target.removeEventListener('keydown', listeners.keydown as EventListener)
406
+ target.removeEventListener('keyup', listeners.keyup as EventListener)
407
+
408
+ this.#targetListeners.delete(target)
409
+ this.#targetRegistrations.delete(target)
410
+ }
411
+
412
+ /**
413
+ * Processes keyboard events for a specific target and event type.
414
+ */
415
+ #processTargetEvent(
416
+ event: KeyboardEvent,
417
+ target: HTMLElement | Document | Window,
418
+ eventType: 'keydown' | 'keyup',
419
+ ): void {
420
+ const targetRegs = this.#targetRegistrations.get(target)
421
+ if (!targetRegs) {
422
+ return
423
+ }
424
+
425
+ for (const id of targetRegs) {
426
+ const registration = this.registrations.state.get(id)
427
+ if (!registration) {
428
+ continue
429
+ }
430
+
431
+ // Check if event originated from or bubbled to this target
432
+ if (!this.#isEventForTarget(event, target)) {
433
+ continue
434
+ }
435
+
436
+ if (!registration.options.enabled) {
437
+ continue
438
+ }
439
+
440
+ // Check if we should ignore input elements (defaults to true)
441
+ if (registration.options.ignoreInputs !== false) {
442
+ if (this.#isInputElement(event.target)) {
443
+ // Don't ignore if the hotkey is explicitly scoped to this input element
444
+ if (event.target !== registration.target) {
445
+ continue
446
+ }
447
+ }
448
+ }
449
+
450
+ // Handle keydown events
451
+ if (eventType === 'keydown') {
452
+ if (registration.options.eventType !== 'keydown') {
453
+ continue
454
+ }
455
+
456
+ // Check if the hotkey matches first
457
+ const matches = matchesKeyboardEvent(
458
+ event,
459
+ registration.parsedHotkey,
460
+ registration.options.platform,
461
+ )
462
+
463
+ if (matches) {
464
+ // Always apply preventDefault/stopPropagation if the hotkey matches,
465
+ // even when requireReset is active and has already fired
466
+ if (registration.options.preventDefault) {
467
+ event.preventDefault()
468
+ }
469
+ if (registration.options.stopPropagation) {
470
+ event.stopPropagation()
471
+ }
472
+
473
+ // Only execute callback if requireReset is not active or hasn't fired yet
474
+ if (!registration.options.requireReset || !registration.hasFired) {
475
+ this.#executeHotkeyCallback(registration, event)
476
+
477
+ // Mark as fired if requireReset is enabled
478
+ if (registration.options.requireReset) {
479
+ registration.hasFired = true
480
+ }
481
+ }
482
+ }
483
+ }
484
+ // Handle keyup events
485
+ else {
486
+ if (registration.options.eventType === 'keyup') {
487
+ if (
488
+ matchesKeyboardEvent(
489
+ event,
490
+ registration.parsedHotkey,
491
+ registration.options.platform,
492
+ )
493
+ ) {
494
+ this.#executeHotkeyCallback(registration, event)
495
+ }
496
+ }
497
+
498
+ // Reset hasFired when any key in the hotkey is released
499
+ if (registration.options.requireReset && registration.hasFired) {
500
+ if (this.#shouldResetRegistration(registration, event)) {
501
+ registration.hasFired = false
502
+ }
503
+ }
504
+ }
505
+ }
506
+ }
507
+
508
+ /**
509
+ * Executes a hotkey callback with proper event handling.
510
+ */
511
+ #executeHotkeyCallback(
512
+ registration: HotkeyRegistration,
513
+ event: KeyboardEvent,
514
+ ): void {
515
+ if (registration.options.preventDefault) {
516
+ event.preventDefault()
517
+ }
518
+ if (registration.options.stopPropagation) {
519
+ event.stopPropagation()
520
+ }
521
+
522
+ registration.triggerCount++
523
+
524
+ // Notify the store so subscribers (e.g. devtools) see the updated count.
525
+ // We create a new Map but keep the same registration reference to preserve
526
+ // identity for mutation-based fields like hasFired.
527
+ this.registrations.setState((prev) => new Map(prev))
528
+
529
+ const context: HotkeyCallbackContext = {
530
+ hotkey: registration.hotkey,
531
+ parsedHotkey: registration.parsedHotkey,
532
+ }
533
+
534
+ registration.callback(event, context)
535
+ }
536
+
537
+ /**
538
+ * Creates a keydown handler for a specific target.
539
+ */
540
+ #createTargetKeyDownHandler(
541
+ target: HTMLElement | Document | Window,
542
+ ): (event: KeyboardEvent) => void {
543
+ return (event: KeyboardEvent) => {
544
+ this.#processTargetEvent(event, target, 'keydown')
545
+ }
546
+ }
547
+
548
+ /**
549
+ * Creates a keyup handler for a specific target.
550
+ */
551
+ #createTargetKeyUpHandler(
552
+ target: HTMLElement | Document | Window,
553
+ ): (event: KeyboardEvent) => void {
554
+ return (event: KeyboardEvent) => {
555
+ this.#processTargetEvent(event, target, 'keyup')
556
+ }
557
+ }
558
+
559
+ /**
560
+ * Checks if an event is for the given target (originated from or bubbled to it).
561
+ */
562
+ #isEventForTarget(
563
+ event: KeyboardEvent,
564
+ target: HTMLElement | Document | Window,
565
+ ): boolean {
566
+ // For Document and Window, check if currentTarget matches
567
+ if (target === document || target === window) {
568
+ return event.currentTarget === target
569
+ }
570
+
571
+ // For HTMLElement, check if event originated from or bubbled to the element
572
+ if (target instanceof HTMLElement) {
573
+ // Check if the event's currentTarget is the target (capturing/bubbling)
574
+ if (event.currentTarget === target) {
575
+ return true
576
+ }
577
+
578
+ // Check if the event's target is a descendant of our target
579
+ if (event.target instanceof Node && target.contains(event.target)) {
580
+ return true
581
+ }
582
+ }
583
+
584
+ return false
585
+ }
586
+
587
+ /**
588
+ * Finds an existing registration with the same hotkey and target.
589
+ */
590
+ #findConflictingRegistration(
591
+ hotkey: Hotkey,
592
+ target: HTMLElement | Document | Window,
593
+ ): HotkeyRegistration | null {
594
+ for (const registration of this.registrations.state.values()) {
595
+ if (registration.hotkey === hotkey && registration.target === target) {
596
+ return registration
597
+ }
598
+ }
599
+ return null
600
+ }
601
+
602
+ /**
603
+ * Handles conflicts between hotkey registrations based on conflict behavior.
604
+ */
605
+ #handleConflict(
606
+ conflictingRegistration: HotkeyRegistration,
607
+ hotkey: Hotkey,
608
+ conflictBehavior: ConflictBehavior,
609
+ ): void {
610
+ if (conflictBehavior === 'allow') {
611
+ return
612
+ }
613
+
614
+ if (conflictBehavior === 'warn') {
615
+ console.warn(
616
+ `Hotkey '${hotkey}' is already registered. Multiple handlers will be triggered. ` +
617
+ `Use conflictBehavior: 'replace' to replace the existing handler, ` +
618
+ `or conflictBehavior: 'allow' to suppress this warning.`,
619
+ )
620
+ return
621
+ }
622
+
623
+ if (conflictBehavior === 'error') {
624
+ throw new Error(
625
+ `Hotkey '${hotkey}' is already registered. ` +
626
+ `Use conflictBehavior: 'replace' to replace the existing handler, ` +
627
+ `or conflictBehavior: 'allow' to allow multiple registrations.`,
628
+ )
629
+ }
630
+
631
+ // At this point, conflictBehavior must be 'replace'
632
+ this.#unregister(conflictingRegistration.id)
633
+ }
634
+
635
+ /**
636
+ * Checks if an element is an input-like element that should be ignored.
637
+ *
638
+ * This includes:
639
+ * - HTMLInputElement (all input types)
640
+ * - HTMLTextAreaElement
641
+ * - HTMLSelectElement
642
+ * - Elements with contentEditable enabled
643
+ */
644
+ #isInputElement(element: EventTarget | null): boolean {
645
+ if (!element) {
646
+ return false
647
+ }
648
+
649
+ // Check for standard input elements
650
+ if (
651
+ element instanceof HTMLInputElement ||
652
+ element instanceof HTMLTextAreaElement ||
653
+ element instanceof HTMLSelectElement
654
+ ) {
655
+ return true
656
+ }
657
+
658
+ // Check for contenteditable elements
659
+ if (element instanceof HTMLElement) {
660
+ const contentEditable = element.contentEditable
661
+ if (contentEditable === 'true' || contentEditable === '') {
662
+ return true
663
+ }
664
+ }
665
+
666
+ return false
667
+ }
668
+
669
+ /**
670
+ * Determines if a registration should be reset based on the keyup event.
671
+ */
672
+ #shouldResetRegistration(
673
+ registration: HotkeyRegistration,
674
+ event: KeyboardEvent,
675
+ ): boolean {
676
+ const parsed = registration.parsedHotkey
677
+ const releasedKey = normalizeKeyName(event.key)
678
+
679
+ // Reset if the main key is released
680
+ // Compare case-insensitively for single-letter keys
681
+ const parsedKeyNormalized =
682
+ parsed.key.length === 1 ? parsed.key.toUpperCase() : parsed.key
683
+ const releasedKeyNormalized =
684
+ releasedKey.length === 1 ? releasedKey.toUpperCase() : releasedKey
685
+
686
+ if (releasedKeyNormalized === parsedKeyNormalized) {
687
+ return true
688
+ }
689
+
690
+ // Reset if any required modifier is released
691
+ // Use normalized key names and check against canonical modifier names
692
+ if (parsed.ctrl && releasedKey === 'Control') {
693
+ return true
694
+ }
695
+ if (parsed.shift && releasedKey === 'Shift') {
696
+ return true
697
+ }
698
+ if (parsed.alt && releasedKey === 'Alt') {
699
+ return true
700
+ }
701
+ if (parsed.meta && releasedKey === 'Meta') {
702
+ return true
703
+ }
704
+
705
+ return false
706
+ }
707
+
708
+ /**
709
+ * Triggers a registration's callback programmatically from devtools.
710
+ * Creates a synthetic KeyboardEvent and invokes the callback.
711
+ *
712
+ * @param id - The registration ID to trigger
713
+ * @returns True if the registration was found and triggered
714
+ */
715
+ triggerRegistration(id: string): boolean {
716
+ const registration = this.registrations.state.get(id)
717
+ if (!registration) {
718
+ return false
719
+ }
720
+
721
+ const parsed = registration.parsedHotkey
722
+ const syntheticEvent = new KeyboardEvent(
723
+ registration.options.eventType ?? 'keydown',
724
+ {
725
+ key: parsed.key,
726
+ ctrlKey: parsed.ctrl,
727
+ shiftKey: parsed.shift,
728
+ altKey: parsed.alt,
729
+ metaKey: parsed.meta,
730
+ bubbles: true,
731
+ cancelable: true,
732
+ },
733
+ )
734
+
735
+ registration.triggerCount++
736
+
737
+ // Notify the store so subscribers (e.g. devtools) see the updated count
738
+ this.registrations.setState((prev) => new Map(prev))
739
+
740
+ registration.callback(syntheticEvent, {
741
+ hotkey: registration.hotkey,
742
+ parsedHotkey: registration.parsedHotkey,
743
+ })
744
+
745
+ return true
746
+ }
747
+
748
+ /**
749
+ * Gets the number of registered hotkeys.
750
+ */
751
+ getRegistrationCount(): number {
752
+ return this.registrations.state.size
753
+ }
754
+
755
+ /**
756
+ * Checks if a specific hotkey is registered.
757
+ *
758
+ * @param hotkey - The hotkey string to check
759
+ * @param target - Optional target element to match (if provided, both hotkey and target must match)
760
+ * @returns True if a matching registration exists
761
+ */
762
+ isRegistered(
763
+ hotkey: Hotkey,
764
+ target?: HTMLElement | Document | Window,
765
+ ): boolean {
766
+ for (const registration of this.registrations.state.values()) {
767
+ if (registration.hotkey === hotkey) {
768
+ // If target is specified, both must match
769
+ if (target === undefined || registration.target === target) {
770
+ return true
771
+ }
772
+ }
773
+ }
774
+ return false
775
+ }
776
+
777
+ /**
778
+ * Destroys the manager and removes all listeners.
779
+ */
780
+ destroy(): void {
781
+ // Remove all target listeners
782
+ for (const target of this.#targetListeners.keys()) {
783
+ this.#removeListenersForTarget(target)
784
+ }
785
+
786
+ this.registrations.setState(() => new Map())
787
+ this.#targetListeners.clear()
788
+ this.#targetRegistrations.clear()
789
+ }
790
+ }
791
+
792
+ /**
793
+ * Gets the singleton HotkeyManager instance.
794
+ * Convenience function for accessing the manager.
795
+ */
796
+ export function getHotkeyManager(): HotkeyManager {
797
+ return HotkeyManager.getInstance()
798
+ }