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