@tanstack/hotkeys 0.8.0 → 0.10.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 (109) hide show
  1. package/README.md +15 -1
  2. package/dist/{manager.utils.js → _event-target.js} +2 -41
  3. package/dist/_keyboard-event.js +75 -0
  4. package/dist/_match.js +48 -0
  5. package/dist/_named-keys.d.ts +6 -0
  6. package/dist/_named-keys.js +65 -0
  7. package/dist/_recorder-chord.js +42 -0
  8. package/dist/_recording-guard.js +55 -0
  9. package/dist/_registration.js +49 -0
  10. package/dist/conflicts.d.ts +36 -0
  11. package/dist/conflicts.js +54 -0
  12. package/dist/constants.d.ts +16 -144
  13. package/dist/constants.js +55 -172
  14. package/dist/display-labels.d.ts +90 -0
  15. package/dist/display-labels.js +112 -0
  16. package/dist/format.d.ts +48 -12
  17. package/dist/format.js +35 -39
  18. package/dist/hint.d.ts +20 -0
  19. package/dist/hint.js +23 -0
  20. package/dist/hotkey-manager.d.ts +14 -12
  21. package/dist/hotkey-manager.js +70 -21
  22. package/dist/hotkey-recorder.d.ts +8 -8
  23. package/dist/hotkey-recorder.js +74 -15
  24. package/dist/hotkey-sequence-recorder.d.ts +13 -9
  25. package/dist/hotkey-sequence-recorder.js +90 -28
  26. package/dist/{hotkey.d.ts → hotkey.types.d.ts} +58 -126
  27. package/dist/index.d.ts +14 -9
  28. package/dist/index.js +10 -6
  29. package/dist/key-state-tracker.d.ts +4 -7
  30. package/dist/key-state-tracker.js +30 -27
  31. package/dist/key.types.d.ts +79 -0
  32. package/dist/match.d.ts +24 -11
  33. package/dist/match.js +19 -30
  34. package/dist/parse.d.ts +18 -15
  35. package/dist/parse.js +39 -22
  36. package/dist/platform.d.ts +41 -0
  37. package/dist/platform.js +52 -0
  38. package/dist/recorder-options.d.ts +31 -0
  39. package/dist/sequence-manager.d.ts +27 -15
  40. package/dist/sequence-manager.js +61 -30
  41. package/dist/validate.d.ts +16 -8
  42. package/dist/validate.js +8 -13
  43. package/package.json +8 -13
  44. package/dist/constants.cjs +0 -535
  45. package/dist/constants.cjs.map +0 -1
  46. package/dist/constants.d.cts +0 -290
  47. package/dist/constants.js.map +0 -1
  48. package/dist/format.cjs +0 -91
  49. package/dist/format.cjs.map +0 -1
  50. package/dist/format.d.cts +0 -60
  51. package/dist/format.js.map +0 -1
  52. package/dist/hotkey-manager.cjs +0 -383
  53. package/dist/hotkey-manager.cjs.map +0 -1
  54. package/dist/hotkey-manager.d.cts +0 -230
  55. package/dist/hotkey-manager.js.map +0 -1
  56. package/dist/hotkey-recorder.cjs +0 -179
  57. package/dist/hotkey-recorder.cjs.map +0 -1
  58. package/dist/hotkey-recorder.d.cts +0 -116
  59. package/dist/hotkey-recorder.js.map +0 -1
  60. package/dist/hotkey-sequence-recorder.cjs +0 -192
  61. package/dist/hotkey-sequence-recorder.cjs.map +0 -1
  62. package/dist/hotkey-sequence-recorder.d.cts +0 -79
  63. package/dist/hotkey-sequence-recorder.js.map +0 -1
  64. package/dist/hotkey.d.cts +0 -301
  65. package/dist/index.cjs +0 -63
  66. package/dist/index.d.cts +0 -13
  67. package/dist/key-state-tracker.cjs +0 -197
  68. package/dist/key-state-tracker.cjs.map +0 -1
  69. package/dist/key-state-tracker.d.cts +0 -107
  70. package/dist/key-state-tracker.js.map +0 -1
  71. package/dist/manager.utils.cjs +0 -115
  72. package/dist/manager.utils.cjs.map +0 -1
  73. package/dist/manager.utils.d.cts +0 -13
  74. package/dist/manager.utils.d.ts +0 -13
  75. package/dist/manager.utils.js.map +0 -1
  76. package/dist/match.cjs +0 -153
  77. package/dist/match.cjs.map +0 -1
  78. package/dist/match.d.cts +0 -85
  79. package/dist/match.js.map +0 -1
  80. package/dist/parse.cjs +0 -228
  81. package/dist/parse.cjs.map +0 -1
  82. package/dist/parse.d.cts +0 -128
  83. package/dist/parse.js.map +0 -1
  84. package/dist/recorder-chord.cjs +0 -18
  85. package/dist/recorder-chord.cjs.map +0 -1
  86. package/dist/recorder-chord.js +0 -18
  87. package/dist/recorder-chord.js.map +0 -1
  88. package/dist/sequence-manager.cjs +0 -415
  89. package/dist/sequence-manager.cjs.map +0 -1
  90. package/dist/sequence-manager.d.cts +0 -157
  91. package/dist/sequence-manager.js.map +0 -1
  92. package/dist/validate.cjs +0 -116
  93. package/dist/validate.cjs.map +0 -1
  94. package/dist/validate.d.cts +0 -56
  95. package/dist/validate.js.map +0 -1
  96. package/src/constants.ts +0 -603
  97. package/src/format.ts +0 -142
  98. package/src/hotkey-manager.ts +0 -759
  99. package/src/hotkey-recorder.ts +0 -272
  100. package/src/hotkey-sequence-recorder.ts +0 -328
  101. package/src/hotkey.ts +0 -436
  102. package/src/index.ts +0 -11
  103. package/src/key-state-tracker.ts +0 -249
  104. package/src/manager.utils.ts +0 -231
  105. package/src/match.ts +0 -259
  106. package/src/parse.ts +0 -328
  107. package/src/recorder-chord.ts +0 -25
  108. package/src/sequence-manager.ts +0 -734
  109. package/src/validate.ts +0 -171
@@ -1,197 +0,0 @@
1
- const require_constants = require('./constants.cjs');
2
- let _tanstack_store = require("@tanstack/store");
3
-
4
- //#region src/key-state-tracker.ts
5
- /**
6
- * Returns the default state for KeyStateTracker.
7
- */
8
- function getDefaultKeyStateTrackerState() {
9
- return {
10
- heldKeys: [],
11
- heldCodes: {}
12
- };
13
- }
14
- /**
15
- * Singleton tracker for currently held keyboard keys.
16
- *
17
- * This class maintains a list of all keys currently being pressed,
18
- * which is useful for:
19
- * - Displaying currently held keys to users
20
- * - Custom shortcut recording for rebinding
21
- * - Complex chord detection
22
- *
23
- * State Management:
24
- * - Uses TanStack Store for reactive state management
25
- * - State can be accessed via `tracker.store.state` when using the class directly
26
- * - When using framework adapters (React), use `useHeldKeys` and `useHeldKeyCodes` hooks for reactive state
27
- *
28
- * @example
29
- * ```ts
30
- * const tracker = KeyStateTracker.getInstance()
31
- *
32
- * // Access state directly
33
- * console.log(tracker.store.state.heldKeys) // ['Control', 'Shift']
34
- *
35
- * // Subscribe to changes with TanStack Store
36
- * const unsubscribe = tracker.store.subscribe(() => {
37
- * console.log('Currently held:', tracker.store.state.heldKeys)
38
- * })
39
- *
40
- * // Check current state
41
- * console.log(tracker.getHeldKeys()) // ['Control', 'Shift']
42
- * console.log(tracker.isKeyHeld('Control')) // true
43
- *
44
- * // Cleanup
45
- * unsubscribe()
46
- * ```
47
- */
48
- var KeyStateTracker = class KeyStateTracker {
49
- static #instance = null;
50
- #heldKeysSet = /* @__PURE__ */ new Set();
51
- #heldCodesMap = /* @__PURE__ */ new Map();
52
- #keydownListener = null;
53
- #keyupListener = null;
54
- #blurListener = null;
55
- constructor() {
56
- this.store = new _tanstack_store.Store(getDefaultKeyStateTrackerState());
57
- this.#setupListeners();
58
- }
59
- /**
60
- * Gets the singleton instance of KeyStateTracker.
61
- */
62
- static getInstance() {
63
- if (!KeyStateTracker.#instance) KeyStateTracker.#instance = new KeyStateTracker();
64
- return KeyStateTracker.#instance;
65
- }
66
- /**
67
- * Resets the singleton instance. Useful for testing.
68
- */
69
- static resetInstance() {
70
- if (KeyStateTracker.#instance) {
71
- KeyStateTracker.#instance.destroy();
72
- KeyStateTracker.#instance = null;
73
- }
74
- }
75
- /**
76
- * Sets up the keyboard event listeners.
77
- */
78
- #setupListeners() {
79
- if (typeof document === "undefined") return;
80
- this.#keydownListener = (event) => {
81
- const key = require_constants.normalizeKeyName(event.key);
82
- if (!this.#heldKeysSet.has(key)) {
83
- this.#heldKeysSet.add(key);
84
- this.#heldCodesMap.set(key, event.code);
85
- this.#syncState();
86
- }
87
- };
88
- this.#keyupListener = (event) => {
89
- const key = require_constants.normalizeKeyName(event.key);
90
- if (this.#heldKeysSet.has(key)) {
91
- this.#heldKeysSet.delete(key);
92
- this.#heldCodesMap.delete(key);
93
- }
94
- if (require_constants.MODIFIER_KEYS.has(key)) {
95
- for (const heldKey of this.#heldKeysSet) if (!require_constants.MODIFIER_KEYS.has(heldKey)) {
96
- this.#heldKeysSet.delete(heldKey);
97
- this.#heldCodesMap.delete(heldKey);
98
- }
99
- }
100
- this.#syncState();
101
- };
102
- this.#blurListener = () => {
103
- if (this.#heldKeysSet.size > 0) {
104
- this.#heldKeysSet.clear();
105
- this.#heldCodesMap.clear();
106
- this.#syncState();
107
- }
108
- };
109
- document.addEventListener("keydown", this.#keydownListener);
110
- document.addEventListener("keyup", this.#keyupListener);
111
- window.addEventListener("blur", this.#blurListener);
112
- }
113
- /**
114
- * Syncs the internal Set to the Store state.
115
- */
116
- #syncState() {
117
- this.store.setState(() => ({
118
- heldKeys: Array.from(this.#heldKeysSet),
119
- heldCodes: Object.fromEntries(this.#heldCodesMap)
120
- }));
121
- }
122
- /**
123
- * Removes the keyboard event listeners.
124
- */
125
- #removeListeners() {
126
- if (typeof document === "undefined") return;
127
- if (this.#keydownListener) {
128
- document.removeEventListener("keydown", this.#keydownListener);
129
- this.#keydownListener = null;
130
- }
131
- if (this.#keyupListener) {
132
- document.removeEventListener("keyup", this.#keyupListener);
133
- this.#keyupListener = null;
134
- }
135
- if (this.#blurListener) {
136
- window.removeEventListener("blur", this.#blurListener);
137
- this.#blurListener = null;
138
- }
139
- }
140
- /**
141
- * Gets an array of currently held key names.
142
- *
143
- * @returns Array of key names currently being pressed
144
- */
145
- getHeldKeys() {
146
- return this.store.state.heldKeys;
147
- }
148
- /**
149
- * Checks if a specific key is currently being held.
150
- *
151
- * @param key - The key name to check (case-insensitive)
152
- * @returns True if the key is currently held
153
- */
154
- isKeyHeld(key) {
155
- const normalizedKey = require_constants.normalizeKeyName(key);
156
- return this.#heldKeysSet.has(normalizedKey);
157
- }
158
- /**
159
- * Checks if any of the given keys are currently held.
160
- *
161
- * @param keys - Array of key names to check
162
- * @returns True if any of the keys are currently held
163
- */
164
- isAnyKeyHeld(keys) {
165
- return keys.some((key) => this.isKeyHeld(key));
166
- }
167
- /**
168
- * Checks if all of the given keys are currently held.
169
- *
170
- * @param keys - Array of key names to check
171
- * @returns True if all of the keys are currently held
172
- */
173
- areAllKeysHeld(keys) {
174
- return keys.every((key) => this.isKeyHeld(key));
175
- }
176
- /**
177
- * Destroys the tracker and removes all listeners.
178
- */
179
- destroy() {
180
- this.#removeListeners();
181
- this.#heldKeysSet.clear();
182
- this.#heldCodesMap.clear();
183
- this.store.setState(() => getDefaultKeyStateTrackerState());
184
- }
185
- };
186
- /**
187
- * Gets the singleton KeyStateTracker instance.
188
- * Convenience function for accessing the tracker.
189
- */
190
- function getKeyStateTracker() {
191
- return KeyStateTracker.getInstance();
192
- }
193
-
194
- //#endregion
195
- exports.KeyStateTracker = KeyStateTracker;
196
- exports.getKeyStateTracker = getKeyStateTracker;
197
- //# sourceMappingURL=key-state-tracker.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"key-state-tracker.cjs","names":["#instance","Store","#setupListeners","#keydownListener","normalizeKeyName","#heldKeysSet","#heldCodesMap","#syncState","#keyupListener","MODIFIER_KEYS","#blurListener","#removeListeners"],"sources":["../src/key-state-tracker.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { MODIFIER_KEYS, normalizeKeyName } from './constants'\n\n/**\n * State interface for the KeyStateTracker.\n */\nexport interface KeyStateTrackerState {\n /**\n * Array of currently held key names (normalized, e.g. \"Control\", \"A\").\n */\n heldKeys: Array<string>\n /**\n * Map from normalized key name to the physical `event.code` (e.g. \"KeyA\", \"ShiftLeft\").\n * Useful for debugging which physical key was pressed.\n */\n heldCodes: Record<string, string>\n}\n\n/**\n * Returns the default state for KeyStateTracker.\n */\nfunction getDefaultKeyStateTrackerState(): KeyStateTrackerState {\n return {\n heldKeys: [],\n heldCodes: {},\n }\n}\n\n/**\n * Singleton tracker for currently held keyboard keys.\n *\n * This class maintains a list of all keys currently being pressed,\n * which is useful for:\n * - Displaying currently held keys to users\n * - Custom shortcut recording for rebinding\n * - Complex chord detection\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - State can be accessed via `tracker.store.state` when using the class directly\n * - When using framework adapters (React), use `useHeldKeys` and `useHeldKeyCodes` hooks for reactive state\n *\n * @example\n * ```ts\n * const tracker = KeyStateTracker.getInstance()\n *\n * // Access state directly\n * console.log(tracker.store.state.heldKeys) // ['Control', 'Shift']\n *\n * // Subscribe to changes with TanStack Store\n * const unsubscribe = tracker.store.subscribe(() => {\n * console.log('Currently held:', tracker.store.state.heldKeys)\n * })\n *\n * // Check current state\n * console.log(tracker.getHeldKeys()) // ['Control', 'Shift']\n * console.log(tracker.isKeyHeld('Control')) // true\n *\n * // Cleanup\n * unsubscribe()\n * ```\n */\nexport class KeyStateTracker {\n static #instance: KeyStateTracker | null = null\n\n /**\n * The TanStack Store instance containing the tracker state.\n * Use this to subscribe to state changes or access current state.\n */\n readonly store: Store<KeyStateTrackerState> = new Store(\n getDefaultKeyStateTrackerState(),\n )\n\n #heldKeysSet: Set<string> = new Set()\n #heldCodesMap: Map<string, string> = new Map()\n #keydownListener: ((event: KeyboardEvent) => void) | null = null\n #keyupListener: ((event: KeyboardEvent) => void) | null = null\n #blurListener: (() => void) | null = null\n\n private constructor() {\n this.#setupListeners()\n }\n\n /**\n * Gets the singleton instance of KeyStateTracker.\n */\n static getInstance(): KeyStateTracker {\n if (!KeyStateTracker.#instance) {\n KeyStateTracker.#instance = new KeyStateTracker()\n }\n return KeyStateTracker.#instance\n }\n\n /**\n * Resets the singleton instance. Useful for testing.\n */\n static resetInstance(): void {\n if (KeyStateTracker.#instance) {\n KeyStateTracker.#instance.destroy()\n KeyStateTracker.#instance = null\n }\n }\n\n /**\n * Sets up the keyboard event listeners.\n */\n #setupListeners(): void {\n if (typeof document === 'undefined') {\n return // SSR safety\n }\n\n this.#keydownListener = (event: KeyboardEvent) => {\n const key = normalizeKeyName(event.key)\n if (!this.#heldKeysSet.has(key)) {\n this.#heldKeysSet.add(key)\n this.#heldCodesMap.set(key, event.code)\n this.#syncState()\n }\n }\n\n this.#keyupListener = (event: KeyboardEvent) => {\n const key = normalizeKeyName(event.key)\n if (this.#heldKeysSet.has(key)) {\n this.#heldKeysSet.delete(key)\n this.#heldCodesMap.delete(key)\n }\n\n // When a modifier key is released, clear any non-modifier keys still\n // marked as held. On macOS, the OS intercepts modifier+key combos\n // (e.g. Cmd+S) and swallows the keyup event for the non-modifier key,\n // leaving it permanently stuck in the held set.\n if (MODIFIER_KEYS.has(key)) {\n for (const heldKey of this.#heldKeysSet) {\n if (!MODIFIER_KEYS.has(heldKey)) {\n this.#heldKeysSet.delete(heldKey)\n this.#heldCodesMap.delete(heldKey)\n }\n }\n }\n\n this.#syncState()\n }\n\n // Clear all keys when window loses focus (keys might be released while not focused)\n this.#blurListener = () => {\n if (this.#heldKeysSet.size > 0) {\n this.#heldKeysSet.clear()\n this.#heldCodesMap.clear()\n this.#syncState()\n }\n }\n\n document.addEventListener('keydown', this.#keydownListener)\n document.addEventListener('keyup', this.#keyupListener)\n window.addEventListener('blur', this.#blurListener)\n }\n\n /**\n * Syncs the internal Set to the Store state.\n */\n #syncState(): void {\n this.store.setState(() => ({\n heldKeys: Array.from(this.#heldKeysSet),\n heldCodes: Object.fromEntries(this.#heldCodesMap),\n }))\n }\n\n /**\n * Removes the keyboard event listeners.\n */\n #removeListeners(): void {\n if (typeof document === 'undefined') {\n return\n }\n\n if (this.#keydownListener) {\n document.removeEventListener('keydown', this.#keydownListener)\n this.#keydownListener = null\n }\n\n if (this.#keyupListener) {\n document.removeEventListener('keyup', this.#keyupListener)\n this.#keyupListener = null\n }\n\n if (this.#blurListener) {\n window.removeEventListener('blur', this.#blurListener)\n this.#blurListener = null\n }\n }\n\n /**\n * Gets an array of currently held key names.\n *\n * @returns Array of key names currently being pressed\n */\n getHeldKeys(): Array<string> {\n return this.store.state.heldKeys\n }\n\n /**\n * Checks if a specific key is currently being held.\n *\n * @param key - The key name to check (case-insensitive)\n * @returns True if the key is currently held\n */\n isKeyHeld(key: string): boolean {\n const normalizedKey = normalizeKeyName(key)\n return this.#heldKeysSet.has(normalizedKey)\n }\n\n /**\n * Checks if any of the given keys are currently held.\n *\n * @param keys - Array of key names to check\n * @returns True if any of the keys are currently held\n */\n isAnyKeyHeld(keys: Array<string>): boolean {\n return keys.some((key) => this.isKeyHeld(key))\n }\n\n /**\n * Checks if all of the given keys are currently held.\n *\n * @param keys - Array of key names to check\n * @returns True if all of the keys are currently held\n */\n areAllKeysHeld(keys: Array<string>): boolean {\n return keys.every((key) => this.isKeyHeld(key))\n }\n\n /**\n * Destroys the tracker and removes all listeners.\n */\n destroy(): void {\n this.#removeListeners()\n this.#heldKeysSet.clear()\n this.#heldCodesMap.clear()\n this.store.setState(() => getDefaultKeyStateTrackerState())\n }\n}\n\n/**\n * Gets the singleton KeyStateTracker instance.\n * Convenience function for accessing the tracker.\n */\nexport function getKeyStateTracker(): KeyStateTracker {\n return KeyStateTracker.getInstance()\n}\n"],"mappings":";;;;;;;AAqBA,SAAS,iCAAuD;AAC9D,QAAO;EACL,UAAU,EAAE;EACZ,WAAW,EAAE;EACd;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCH,IAAa,kBAAb,MAAa,gBAAgB;CAC3B,QAAOA,WAAoC;CAU3C,+BAA4B,IAAI,KAAK;CACrC,gCAAqC,IAAI,KAAK;CAC9C,mBAA4D;CAC5D,iBAA0D;CAC1D,gBAAqC;CAErC,AAAQ,cAAc;eAVwB,IAAIC,sBAChD,gCAAgC,CACjC;AASC,QAAKC,gBAAiB;;;;;CAMxB,OAAO,cAA+B;AACpC,MAAI,CAAC,iBAAgBF,SACnB,kBAAgBA,WAAY,IAAI,iBAAiB;AAEnD,SAAO,iBAAgBA;;;;;CAMzB,OAAO,gBAAsB;AAC3B,MAAI,iBAAgBA,UAAW;AAC7B,oBAAgBA,SAAU,SAAS;AACnC,oBAAgBA,WAAY;;;;;;CAOhC,kBAAwB;AACtB,MAAI,OAAO,aAAa,YACtB;AAGF,QAAKG,mBAAoB,UAAyB;GAChD,MAAM,MAAMC,mCAAiB,MAAM,IAAI;AACvC,OAAI,CAAC,MAAKC,YAAa,IAAI,IAAI,EAAE;AAC/B,UAAKA,YAAa,IAAI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CAS1B,UAAU,KAAsB;EAC9B,MAAM,gBAAgBN,mCAAiB,IAAI;AAC3C,SAAO,MAAKC,YAAa,IAAI,cAAc;;;;;;;;CAS7C,aAAa,MAA8B;AACzC,SAAO,KAAK,MAAM,QAAQ,KAAK,UAAU,IAAI,CAAC;;;;;;;;CAShD,eAAe,MAA8B;AAC3C,SAAO,KAAK,OAAO,QAAQ,KAAK,UAAU,IAAI,CAAC;;;;;CAMjD,UAAgB;AACd,QAAKM,iBAAkB;AACvB,QAAKN,YAAa,OAAO;AACzB,QAAKC,aAAc,OAAO;AAC1B,OAAK,MAAM,eAAe,gCAAgC,CAAC;;;;;;;AAQ/D,SAAgB,qBAAsC;AACpD,QAAO,gBAAgB,aAAa"}
@@ -1,107 +0,0 @@
1
- import { Store } from "@tanstack/store";
2
-
3
- //#region src/key-state-tracker.d.ts
4
- /**
5
- * State interface for the KeyStateTracker.
6
- */
7
- 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
- * Singleton tracker for currently held keyboard keys.
20
- *
21
- * This class maintains a list of all keys currently being pressed,
22
- * which is useful for:
23
- * - Displaying currently held keys to users
24
- * - Custom shortcut recording for rebinding
25
- * - Complex chord detection
26
- *
27
- * State Management:
28
- * - Uses TanStack Store for reactive state management
29
- * - State can be accessed via `tracker.store.state` when using the class directly
30
- * - When using framework adapters (React), use `useHeldKeys` and `useHeldKeyCodes` hooks for reactive state
31
- *
32
- * @example
33
- * ```ts
34
- * const tracker = KeyStateTracker.getInstance()
35
- *
36
- * // Access state directly
37
- * console.log(tracker.store.state.heldKeys) // ['Control', 'Shift']
38
- *
39
- * // Subscribe to changes with TanStack Store
40
- * const unsubscribe = tracker.store.subscribe(() => {
41
- * console.log('Currently held:', tracker.store.state.heldKeys)
42
- * })
43
- *
44
- * // Check current state
45
- * console.log(tracker.getHeldKeys()) // ['Control', 'Shift']
46
- * console.log(tracker.isKeyHeld('Control')) // true
47
- *
48
- * // Cleanup
49
- * unsubscribe()
50
- * ```
51
- */
52
- declare class KeyStateTracker {
53
- #private;
54
- /**
55
- * The TanStack Store instance containing the tracker state.
56
- * Use this to subscribe to state changes or access current state.
57
- */
58
- readonly store: Store<KeyStateTrackerState>;
59
- private constructor();
60
- /**
61
- * Gets the singleton instance of KeyStateTracker.
62
- */
63
- static getInstance(): KeyStateTracker;
64
- /**
65
- * Resets the singleton instance. Useful for testing.
66
- */
67
- static resetInstance(): void;
68
- /**
69
- * Gets an array of currently held key names.
70
- *
71
- * @returns Array of key names currently being pressed
72
- */
73
- getHeldKeys(): Array<string>;
74
- /**
75
- * Checks if a specific key is currently being held.
76
- *
77
- * @param key - The key name to check (case-insensitive)
78
- * @returns True if the key is currently held
79
- */
80
- isKeyHeld(key: string): boolean;
81
- /**
82
- * Checks if any of the given keys are currently held.
83
- *
84
- * @param keys - Array of key names to check
85
- * @returns True if any of the keys are currently held
86
- */
87
- isAnyKeyHeld(keys: Array<string>): boolean;
88
- /**
89
- * Checks if all of the given keys are currently held.
90
- *
91
- * @param keys - Array of key names to check
92
- * @returns True if all of the keys are currently held
93
- */
94
- areAllKeysHeld(keys: Array<string>): boolean;
95
- /**
96
- * Destroys the tracker and removes all listeners.
97
- */
98
- destroy(): void;
99
- }
100
- /**
101
- * Gets the singleton KeyStateTracker instance.
102
- * Convenience function for accessing the tracker.
103
- */
104
- declare function getKeyStateTracker(): KeyStateTracker;
105
- //#endregion
106
- export { KeyStateTracker, KeyStateTrackerState, getKeyStateTracker };
107
- //# sourceMappingURL=key-state-tracker.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"key-state-tracker.js","names":["#instance","#setupListeners","#keydownListener","#heldKeysSet","#heldCodesMap","#syncState","#keyupListener","#blurListener","#removeListeners"],"sources":["../src/key-state-tracker.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { MODIFIER_KEYS, normalizeKeyName } from './constants'\n\n/**\n * State interface for the KeyStateTracker.\n */\nexport interface KeyStateTrackerState {\n /**\n * Array of currently held key names (normalized, e.g. \"Control\", \"A\").\n */\n heldKeys: Array<string>\n /**\n * Map from normalized key name to the physical `event.code` (e.g. \"KeyA\", \"ShiftLeft\").\n * Useful for debugging which physical key was pressed.\n */\n heldCodes: Record<string, string>\n}\n\n/**\n * Returns the default state for KeyStateTracker.\n */\nfunction getDefaultKeyStateTrackerState(): KeyStateTrackerState {\n return {\n heldKeys: [],\n heldCodes: {},\n }\n}\n\n/**\n * Singleton tracker for currently held keyboard keys.\n *\n * This class maintains a list of all keys currently being pressed,\n * which is useful for:\n * - Displaying currently held keys to users\n * - Custom shortcut recording for rebinding\n * - Complex chord detection\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - State can be accessed via `tracker.store.state` when using the class directly\n * - When using framework adapters (React), use `useHeldKeys` and `useHeldKeyCodes` hooks for reactive state\n *\n * @example\n * ```ts\n * const tracker = KeyStateTracker.getInstance()\n *\n * // Access state directly\n * console.log(tracker.store.state.heldKeys) // ['Control', 'Shift']\n *\n * // Subscribe to changes with TanStack Store\n * const unsubscribe = tracker.store.subscribe(() => {\n * console.log('Currently held:', tracker.store.state.heldKeys)\n * })\n *\n * // Check current state\n * console.log(tracker.getHeldKeys()) // ['Control', 'Shift']\n * console.log(tracker.isKeyHeld('Control')) // true\n *\n * // Cleanup\n * unsubscribe()\n * ```\n */\nexport class KeyStateTracker {\n static #instance: KeyStateTracker | null = null\n\n /**\n * The TanStack Store instance containing the tracker state.\n * Use this to subscribe to state changes or access current state.\n */\n readonly store: Store<KeyStateTrackerState> = new Store(\n getDefaultKeyStateTrackerState(),\n )\n\n #heldKeysSet: Set<string> = new Set()\n #heldCodesMap: Map<string, string> = new Map()\n #keydownListener: ((event: KeyboardEvent) => void) | null = null\n #keyupListener: ((event: KeyboardEvent) => void) | null = null\n #blurListener: (() => void) | null = null\n\n private constructor() {\n this.#setupListeners()\n }\n\n /**\n * Gets the singleton instance of KeyStateTracker.\n */\n static getInstance(): KeyStateTracker {\n if (!KeyStateTracker.#instance) {\n KeyStateTracker.#instance = new KeyStateTracker()\n }\n return KeyStateTracker.#instance\n }\n\n /**\n * Resets the singleton instance. Useful for testing.\n */\n static resetInstance(): void {\n if (KeyStateTracker.#instance) {\n KeyStateTracker.#instance.destroy()\n KeyStateTracker.#instance = null\n }\n }\n\n /**\n * Sets up the keyboard event listeners.\n */\n #setupListeners(): void {\n if (typeof document === 'undefined') {\n return // SSR safety\n }\n\n this.#keydownListener = (event: KeyboardEvent) => {\n const key = normalizeKeyName(event.key)\n if (!this.#heldKeysSet.has(key)) {\n this.#heldKeysSet.add(key)\n this.#heldCodesMap.set(key, event.code)\n this.#syncState()\n }\n }\n\n this.#keyupListener = (event: KeyboardEvent) => {\n const key = normalizeKeyName(event.key)\n if (this.#heldKeysSet.has(key)) {\n this.#heldKeysSet.delete(key)\n this.#heldCodesMap.delete(key)\n }\n\n // When a modifier key is released, clear any non-modifier keys still\n // marked as held. On macOS, the OS intercepts modifier+key combos\n // (e.g. Cmd+S) and swallows the keyup event for the non-modifier key,\n // leaving it permanently stuck in the held set.\n if (MODIFIER_KEYS.has(key)) {\n for (const heldKey of this.#heldKeysSet) {\n if (!MODIFIER_KEYS.has(heldKey)) {\n this.#heldKeysSet.delete(heldKey)\n this.#heldCodesMap.delete(heldKey)\n }\n }\n }\n\n this.#syncState()\n }\n\n // Clear all keys when window loses focus (keys might be released while not focused)\n this.#blurListener = () => {\n if (this.#heldKeysSet.size > 0) {\n this.#heldKeysSet.clear()\n this.#heldCodesMap.clear()\n this.#syncState()\n }\n }\n\n document.addEventListener('keydown', this.#keydownListener)\n document.addEventListener('keyup', this.#keyupListener)\n window.addEventListener('blur', this.#blurListener)\n }\n\n /**\n * Syncs the internal Set to the Store state.\n */\n #syncState(): void {\n this.store.setState(() => ({\n heldKeys: Array.from(this.#heldKeysSet),\n heldCodes: Object.fromEntries(this.#heldCodesMap),\n }))\n }\n\n /**\n * Removes the keyboard event listeners.\n */\n #removeListeners(): void {\n if (typeof document === 'undefined') {\n return\n }\n\n if (this.#keydownListener) {\n document.removeEventListener('keydown', this.#keydownListener)\n this.#keydownListener = null\n }\n\n if (this.#keyupListener) {\n document.removeEventListener('keyup', this.#keyupListener)\n this.#keyupListener = null\n }\n\n if (this.#blurListener) {\n window.removeEventListener('blur', this.#blurListener)\n this.#blurListener = null\n }\n }\n\n /**\n * Gets an array of currently held key names.\n *\n * @returns Array of key names currently being pressed\n */\n getHeldKeys(): Array<string> {\n return this.store.state.heldKeys\n }\n\n /**\n * Checks if a specific key is currently being held.\n *\n * @param key - The key name to check (case-insensitive)\n * @returns True if the key is currently held\n */\n isKeyHeld(key: string): boolean {\n const normalizedKey = normalizeKeyName(key)\n return this.#heldKeysSet.has(normalizedKey)\n }\n\n /**\n * Checks if any of the given keys are currently held.\n *\n * @param keys - Array of key names to check\n * @returns True if any of the keys are currently held\n */\n isAnyKeyHeld(keys: Array<string>): boolean {\n return keys.some((key) => this.isKeyHeld(key))\n }\n\n /**\n * Checks if all of the given keys are currently held.\n *\n * @param keys - Array of key names to check\n * @returns True if all of the keys are currently held\n */\n areAllKeysHeld(keys: Array<string>): boolean {\n return keys.every((key) => this.isKeyHeld(key))\n }\n\n /**\n * Destroys the tracker and removes all listeners.\n */\n destroy(): void {\n this.#removeListeners()\n this.#heldKeysSet.clear()\n this.#heldCodesMap.clear()\n this.store.setState(() => getDefaultKeyStateTrackerState())\n }\n}\n\n/**\n * Gets the singleton KeyStateTracker instance.\n * Convenience function for accessing the tracker.\n */\nexport function getKeyStateTracker(): KeyStateTracker {\n return KeyStateTracker.getInstance()\n}\n"],"mappings":";;;;;;;AAqBA,SAAS,iCAAuD;AAC9D,QAAO;EACL,UAAU,EAAE;EACZ,WAAW,EAAE;EACd;;;;;;;;;;;;;;;;;;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UAAKC,WAAY;;;AAIrB,WAAS,iBAAiB,WAAW,MAAKH,gBAAiB;AAC3D,WAAS,iBAAiB,SAAS,MAAKI,cAAe;AACvD,SAAO,iBAAiB,QAAQ,MAAKC,aAAc;;;;;CAMrD,aAAmB;AACjB,OAAK,MAAM,gBAAgB;GACzB,UAAU,MAAM,KAAK,MAAKJ,YAAa;GACvC,WAAW,OAAO,YAAY,MAAKC,aAAc;GAClD,EAAE;;;;;CAML,mBAAyB;AACvB,MAAI,OAAO,aAAa,YACtB;AAGF,MAAI,MAAKF,iBAAkB;AACzB,YAAS,oBAAoB,WAAW,MAAKA,gBAAiB;AAC9D,SAAKA,kBAAmB;;AAG1B,MAAI,MAAKI,eAAgB;AACvB,YAAS,oBAAoB,SAAS,MAAKA,cAAe;AAC1D,SAAKA,gBAAiB;;AAGxB,MAAI,MAAKC,cAAe;AACtB,UAAO,oBAAoB,QAAQ,MAAKA,aAAc;AACtD,SAAKA,eAAgB;;;;;;;;CASzB,cAA6B;AAC3B,SAAO,KAAK,MAAM,MAAM;;;;;;;;CAS1B,UAAU,KAAsB;EAC9B,MAAM,gBAAgB,iBAAiB,IAAI;AAC3C,SAAO,MAAKJ,YAAa,IAAI,cAAc;;;;;;;;CAS7C,aAAa,MAA8B;AACzC,SAAO,KAAK,MAAM,QAAQ,KAAK,UAAU,IAAI,CAAC;;;;;;;;CAShD,eAAe,MAA8B;AAC3C,SAAO,KAAK,OAAO,QAAQ,KAAK,UAAU,IAAI,CAAC;;;;;CAMjD,UAAgB;AACd,QAAKK,iBAAkB;AACvB,QAAKL,YAAa,OAAO;AACzB,QAAKC,aAAc,OAAO;AAC1B,OAAK,MAAM,eAAe,gCAAgC,CAAC;;;;;;;AAQ/D,SAAgB,qBAAsC;AACpD,QAAO,gBAAgB,aAAa"}
@@ -1,115 +0,0 @@
1
-
2
- //#region src/manager.utils.ts
3
- /**
4
- * Default options for hotkey/sequence registration.
5
- * Omitted: platform, target (resolved at registration), requireReset (HotkeyManager only).
6
- */
7
- const defaultHotkeyOptions = {
8
- preventDefault: true,
9
- stopPropagation: true,
10
- eventType: "keydown",
11
- enabled: true,
12
- ignoreInputs: true,
13
- conflictBehavior: "warn"
14
- };
15
- /**
16
- * Computes the default ignoreInputs value based on the hotkey.
17
- * Ctrl/Meta shortcuts and Escape fire in inputs; single keys and Shift/Alt combos are ignored.
18
- */
19
- function getDefaultIgnoreInputs(parsedHotkey) {
20
- if (parsedHotkey.ctrl || parsedHotkey.meta) return false;
21
- if (parsedHotkey.key === "Escape") return false;
22
- return true;
23
- }
24
- /**
25
- * Checks if an element is an input-like element that should be ignored for hotkeys.
26
- *
27
- * This includes:
28
- * - HTMLInputElement (all input types except button, submit, reset)
29
- * - HTMLTextAreaElement
30
- * - HTMLSelectElement
31
- * - Elements with contentEditable enabled
32
- *
33
- * Button-type inputs (button, submit, reset) are excluded so hotkeys like
34
- * Mod+S and Escape fire when the user has tabbed to a form button.
35
- */
36
- function isInputElement(element) {
37
- if (!element) return false;
38
- if (element instanceof HTMLInputElement) {
39
- const type = element.type.toLowerCase();
40
- if (type === "button" || type === "submit" || type === "reset") return false;
41
- return true;
42
- }
43
- if (element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) return true;
44
- if (element instanceof HTMLElement && element.isContentEditable) return true;
45
- return false;
46
- }
47
- /**
48
- * Returns the focused element for the document associated with where hotkey
49
- * listeners are attached (listener root). Use this instead of the global
50
- * `document.activeElement` so registrations scoped to an iframe (or another
51
- * document) read focus from the correct tree.
52
- */
53
- function getActiveElementForListenerTarget(target) {
54
- if (typeof document === "undefined") return null;
55
- if (typeof Document !== "undefined" && target instanceof Document || typeof Node !== "undefined" && target.nodeType === Node.DOCUMENT_NODE) return target.activeElement;
56
- if (typeof HTMLElement !== "undefined" && target instanceof HTMLElement) return target.ownerDocument.activeElement ?? null;
57
- return target.document.activeElement ?? null;
58
- }
59
- /**
60
- * Returns whether an event should be ignored because it originated from an
61
- * input-like element other than the registration target.
62
- *
63
- * This checks:
64
- * - the currently focused element for the listener target
65
- * - the event's composed path (for shadow DOM)
66
- * - the event target as a final fallback
67
- */
68
- function shouldIgnoreInputEvent(event, listenerTarget, registrationTarget) {
69
- const focused = getActiveElementForListenerTarget(listenerTarget);
70
- if (focused && isInputElement(focused) && focused !== registrationTarget) return true;
71
- if (event.composedPath().some((element) => isInputElement(element) && element !== registrationTarget)) return true;
72
- return isInputElement(event.target) && event.target !== registrationTarget;
73
- }
74
- /**
75
- * Checks if an event is for the given target (originated from or bubbled to it).
76
- *
77
- * For document/window targets, also accepts document.documentElement as currentTarget
78
- * to handle Brave and other browsers where currentTarget may be documentElement
79
- * instead of document when listeners are attached to document.
80
- */
81
- function isEventForTarget(event, target) {
82
- if (target === document || target === window) return event.currentTarget === target || event.currentTarget === document.documentElement;
83
- if (target === window) return event.currentTarget === window || event.currentTarget === document || event.currentTarget === document.documentElement;
84
- if (target instanceof HTMLElement) {
85
- if (event.currentTarget === target) return true;
86
- if (event.target instanceof Node && target.contains(event.target)) return true;
87
- }
88
- return false;
89
- }
90
- /**
91
- * Handles conflicts between registrations based on conflict behavior.
92
- *
93
- * @param conflictingId - The ID of the conflicting registration
94
- * @param keyDisplay - Display string for the conflicting key/sequence (for error messages)
95
- * @param conflictBehavior - How to handle the conflict
96
- * @param unregister - Function to unregister by ID
97
- */
98
- function handleConflict(conflictingId, keyDisplay, conflictBehavior, unregister) {
99
- if (conflictBehavior === "allow") return;
100
- if (conflictBehavior === "warn") {
101
- console.warn(`'${keyDisplay}' is already registered. Multiple handlers will be triggered. Use conflictBehavior: 'replace' to replace the existing handler, or conflictBehavior: 'allow' to suppress this warning.`);
102
- return;
103
- }
104
- if (conflictBehavior === "error") throw new Error(`'${keyDisplay}' is already registered. Use conflictBehavior: 'replace' to replace the existing handler, or conflictBehavior: 'allow' to allow multiple registrations.`);
105
- unregister(conflictingId);
106
- }
107
-
108
- //#endregion
109
- exports.defaultHotkeyOptions = defaultHotkeyOptions;
110
- exports.getDefaultIgnoreInputs = getDefaultIgnoreInputs;
111
- exports.handleConflict = handleConflict;
112
- exports.isEventForTarget = isEventForTarget;
113
- exports.isInputElement = isInputElement;
114
- exports.shouldIgnoreInputEvent = shouldIgnoreInputEvent;
115
- //# sourceMappingURL=manager.utils.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"manager.utils.cjs","names":[],"sources":["../src/manager.utils.ts"],"sourcesContent":["import type { ParsedHotkey } from './hotkey'\n\n/**\n * Behavior when registering a hotkey/sequence that conflicts with an existing registration.\n *\n * - `'warn'` - Log a warning to the console but allow both registrations (default)\n * - `'error'` - Throw an error and prevent the new registration\n * - `'replace'` - Unregister the existing registration and register the new one\n * - `'allow'` - Allow multiple registrations without warning\n */\nexport type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow'\n\n/**\n * Default options for hotkey/sequence registration.\n * Omitted: platform, target (resolved at registration), requireReset (HotkeyManager only).\n */\nexport const defaultHotkeyOptions = {\n preventDefault: true,\n stopPropagation: true,\n eventType: 'keydown' as const,\n enabled: true,\n ignoreInputs: true,\n conflictBehavior: 'warn' as ConflictBehavior,\n}\n\n/**\n * Computes the default ignoreInputs value based on the hotkey.\n * Ctrl/Meta shortcuts and Escape fire in inputs; single keys and Shift/Alt combos are ignored.\n */\nexport function getDefaultIgnoreInputs(parsedHotkey: ParsedHotkey): boolean {\n if (parsedHotkey.ctrl || parsedHotkey.meta) return false // Mod+S, Ctrl+C, etc.\n if (parsedHotkey.key === 'Escape') return false // Close modal, etc.\n return true // Single keys, Shift+key, Alt+key\n}\n\n/**\n * Checks if an element is an input-like element that should be ignored for hotkeys.\n *\n * This includes:\n * - HTMLInputElement (all input types except button, submit, reset)\n * - HTMLTextAreaElement\n * - HTMLSelectElement\n * - Elements with contentEditable enabled\n *\n * Button-type inputs (button, submit, reset) are excluded so hotkeys like\n * Mod+S and Escape fire when the user has tabbed to a form button.\n */\nexport function isInputElement(element: EventTarget | null): boolean {\n if (!element) {\n return false\n }\n\n if (element instanceof HTMLInputElement) {\n const type = element.type.toLowerCase()\n if (type === 'button' || type === 'submit' || type === 'reset') {\n return false\n }\n return true\n }\n\n if (\n element instanceof HTMLTextAreaElement ||\n element instanceof HTMLSelectElement\n ) {\n return true\n }\n\n // Check for contenteditable elements (includes \"true\", \"\", \"plaintext-only\",\n // and inherited contenteditable from ancestor elements)\n if (element instanceof HTMLElement && element.isContentEditable) {\n return true\n }\n\n return false\n}\n\n/**\n * Returns the focused element for the document associated with where hotkey\n * listeners are attached (listener root). Use this instead of the global\n * `document.activeElement` so registrations scoped to an iframe (or another\n * document) read focus from the correct tree.\n */\nexport function getActiveElementForListenerTarget(\n target: HTMLElement | Document | Window,\n): Element | null {\n if (typeof document === 'undefined') {\n return null\n }\n\n // Document first (nodeType covers environments where `document instanceof Document` is false)\n if (\n (typeof Document !== 'undefined' && target instanceof Document) ||\n (typeof Node !== 'undefined' &&\n (target as Node).nodeType === Node.DOCUMENT_NODE)\n ) {\n return (target as Document).activeElement\n }\n\n if (typeof HTMLElement !== 'undefined' && target instanceof HTMLElement) {\n return target.ownerDocument.activeElement ?? null\n }\n\n // Window (global or iframe): avoid relying on `instanceof Window` alone (test DOM quirks)\n return (target as Window).document.activeElement ?? null\n}\n\n/**\n * Returns whether an event should be ignored because it originated from an\n * input-like element other than the registration target.\n *\n * This checks:\n * - the currently focused element for the listener target\n * - the event's composed path (for shadow DOM)\n * - the event target as a final fallback\n */\nexport function shouldIgnoreInputEvent(\n event: KeyboardEvent,\n listenerTarget: HTMLElement | Document | Window,\n registrationTarget: HTMLElement | Document | Window,\n): boolean {\n const focused = getActiveElementForListenerTarget(listenerTarget)\n if (focused && isInputElement(focused) && focused !== registrationTarget) {\n return true\n }\n\n if (\n event\n .composedPath()\n .some(\n (element) => isInputElement(element) && element !== registrationTarget,\n )\n ) {\n return true\n }\n\n return isInputElement(event.target) && event.target !== registrationTarget\n}\n\n/**\n * Checks if an event is for the given target (originated from or bubbled to it).\n *\n * For document/window targets, also accepts document.documentElement as currentTarget\n * to handle Brave and other browsers where currentTarget may be documentElement\n * instead of document when listeners are attached to document.\n */\nexport function isEventForTarget(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n): boolean {\n // For Document and Window, verify that our handler was indeed called for this target.\n //\n // Browser compatibility note:\n // Per the DOM spec, event.currentTarget should equal the element the listener was\n // attached to. However, some Chromium-based browsers (notably Brave) exhibit\n // non-standard behavior where event.currentTarget is set to document.documentElement\n // (<html>) instead of document when a listener is attached to document.\n // This may be related to privacy/fingerprinting protections.\n //\n // To ensure cross-browser compatibility, we accept both the expected target\n // and document.documentElement as valid currentTarget values.\n // See: https://dom.spec.whatwg.org/#dom-event-currenttarget\n if (target === document || target === window) {\n return (\n event.currentTarget === target ||\n event.currentTarget === document.documentElement\n )\n }\n\n // For Window, accept window, document, or document.documentElement (browser quirks)\n if (target === window) {\n return (\n event.currentTarget === window ||\n event.currentTarget === document ||\n event.currentTarget === document.documentElement\n )\n }\n\n // For HTMLElement, check if event originated from or bubbled to the element\n if (target instanceof HTMLElement) {\n // Check if the event's currentTarget is the target (capturing/bubbling)\n if (event.currentTarget === target) {\n return true\n }\n\n // Check if the event's target is a descendant of our target\n if (event.target instanceof Node && target.contains(event.target)) {\n return true\n }\n }\n\n return false\n}\n\n/**\n * Handles conflicts between registrations based on conflict behavior.\n *\n * @param conflictingId - The ID of the conflicting registration\n * @param keyDisplay - Display string for the conflicting key/sequence (for error messages)\n * @param conflictBehavior - How to handle the conflict\n * @param unregister - Function to unregister by ID\n */\nexport function handleConflict(\n conflictingId: string,\n keyDisplay: string,\n conflictBehavior: ConflictBehavior,\n unregister: (id: string) => void,\n): void {\n if (conflictBehavior === 'allow') {\n return\n }\n\n if (conflictBehavior === 'warn') {\n console.warn(\n `'${keyDisplay}' is already registered. Multiple handlers will be triggered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to suppress this warning.`,\n )\n return\n }\n\n if (conflictBehavior === 'error') {\n throw new Error(\n `'${keyDisplay}' is already registered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to allow multiple registrations.`,\n )\n }\n\n // At this point, conflictBehavior must be 'replace'\n unregister(conflictingId)\n}\n"],"mappings":";;;;;;AAgBA,MAAa,uBAAuB;CAClC,gBAAgB;CAChB,iBAAiB;CACjB,WAAW;CACX,SAAS;CACT,cAAc;CACd,kBAAkB;CACnB;;;;;AAMD,SAAgB,uBAAuB,cAAqC;AAC1E,KAAI,aAAa,QAAQ,aAAa,KAAM,QAAO;AACnD,KAAI,aAAa,QAAQ,SAAU,QAAO;AAC1C,QAAO;;;;;;;;;;;;;;AAeT,SAAgB,eAAe,SAAsC;AACnE,KAAI,CAAC,QACH,QAAO;AAGT,KAAI,mBAAmB,kBAAkB;EACvC,MAAM,OAAO,QAAQ,KAAK,aAAa;AACvC,MAAI,SAAS,YAAY,SAAS,YAAY,SAAS,QACrD,QAAO;AAET,SAAO;;AAGT,KACE,mBAAmB,uBACnB,mBAAmB,kBAEnB,QAAO;AAKT,KAAI,mBAAmB,eAAe,QAAQ,kBAC5C,QAAO;AAGT,QAAO;;;;;;;;AAST,SAAgB,kCACd,QACgB;AAChB,KAAI,OAAO,aAAa,YACtB,QAAO;AAIT,KACG,OAAO,aAAa,eAAe,kBAAkB,YACrD,OAAO,SAAS,eACd,OAAgB,aAAa,KAAK,cAErC,QAAQ,OAAoB;AAG9B,KAAI,OAAO,gBAAgB,eAAe,kBAAkB,YAC1D,QAAO,OAAO,cAAc,iBAAiB;AAI/C,QAAQ,OAAkB,SAAS,iBAAiB;;;;;;;;;;;AAYtD,SAAgB,uBACd,OACA,gBACA,oBACS;CACT,MAAM,UAAU,kCAAkC,eAAe;AACjE,KAAI,WAAW,eAAe,QAAQ,IAAI,YAAY,mBACpD,QAAO;AAGT,KACE,MACG,cAAc,CACd,MACE,YAAY,eAAe,QAAQ,IAAI,YAAY,mBACrD,CAEH,QAAO;AAGT,QAAO,eAAe,MAAM,OAAO,IAAI,MAAM,WAAW;;;;;;;;;AAU1D,SAAgB,iBACd,OACA,QACS;AAaT,KAAI,WAAW,YAAY,WAAW,OACpC,QACE,MAAM,kBAAkB,UACxB,MAAM,kBAAkB,SAAS;AAKrC,KAAI,WAAW,OACb,QACE,MAAM,kBAAkB,UACxB,MAAM,kBAAkB,YACxB,MAAM,kBAAkB,SAAS;AAKrC,KAAI,kBAAkB,aAAa;AAEjC,MAAI,MAAM,kBAAkB,OAC1B,QAAO;AAIT,MAAI,MAAM,kBAAkB,QAAQ,OAAO,SAAS,MAAM,OAAO,CAC/D,QAAO;;AAIX,QAAO;;;;;;;;;;AAWT,SAAgB,eACd,eACA,YACA,kBACA,YACM;AACN,KAAI,qBAAqB,QACvB;AAGF,KAAI,qBAAqB,QAAQ;AAC/B,UAAQ,KACN,IAAI,WAAW,uLAGhB;AACD;;AAGF,KAAI,qBAAqB,QACvB,OAAM,IAAI,MACR,IAAI,WAAW,yJAGhB;AAIH,YAAW,cAAc"}
@@ -1,13 +0,0 @@
1
- //#region src/manager.utils.d.ts
2
- /**
3
- * Behavior when registering a hotkey/sequence that conflicts with an existing registration.
4
- *
5
- * - `'warn'` - Log a warning to the console but allow both registrations (default)
6
- * - `'error'` - Throw an error and prevent the new registration
7
- * - `'replace'` - Unregister the existing registration and register the new one
8
- * - `'allow'` - Allow multiple registrations without warning
9
- */
10
- type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow';
11
- //#endregion
12
- export { ConflictBehavior };
13
- //# sourceMappingURL=manager.utils.d.cts.map
@@ -1,13 +0,0 @@
1
- //#region src/manager.utils.d.ts
2
- /**
3
- * Behavior when registering a hotkey/sequence that conflicts with an existing registration.
4
- *
5
- * - `'warn'` - Log a warning to the console but allow both registrations (default)
6
- * - `'error'` - Throw an error and prevent the new registration
7
- * - `'replace'` - Unregister the existing registration and register the new one
8
- * - `'allow'` - Allow multiple registrations without warning
9
- */
10
- type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow';
11
- //#endregion
12
- export { ConflictBehavior };
13
- //# sourceMappingURL=manager.utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"manager.utils.js","names":[],"sources":["../src/manager.utils.ts"],"sourcesContent":["import type { ParsedHotkey } from './hotkey'\n\n/**\n * Behavior when registering a hotkey/sequence that conflicts with an existing registration.\n *\n * - `'warn'` - Log a warning to the console but allow both registrations (default)\n * - `'error'` - Throw an error and prevent the new registration\n * - `'replace'` - Unregister the existing registration and register the new one\n * - `'allow'` - Allow multiple registrations without warning\n */\nexport type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow'\n\n/**\n * Default options for hotkey/sequence registration.\n * Omitted: platform, target (resolved at registration), requireReset (HotkeyManager only).\n */\nexport const defaultHotkeyOptions = {\n preventDefault: true,\n stopPropagation: true,\n eventType: 'keydown' as const,\n enabled: true,\n ignoreInputs: true,\n conflictBehavior: 'warn' as ConflictBehavior,\n}\n\n/**\n * Computes the default ignoreInputs value based on the hotkey.\n * Ctrl/Meta shortcuts and Escape fire in inputs; single keys and Shift/Alt combos are ignored.\n */\nexport function getDefaultIgnoreInputs(parsedHotkey: ParsedHotkey): boolean {\n if (parsedHotkey.ctrl || parsedHotkey.meta) return false // Mod+S, Ctrl+C, etc.\n if (parsedHotkey.key === 'Escape') return false // Close modal, etc.\n return true // Single keys, Shift+key, Alt+key\n}\n\n/**\n * Checks if an element is an input-like element that should be ignored for hotkeys.\n *\n * This includes:\n * - HTMLInputElement (all input types except button, submit, reset)\n * - HTMLTextAreaElement\n * - HTMLSelectElement\n * - Elements with contentEditable enabled\n *\n * Button-type inputs (button, submit, reset) are excluded so hotkeys like\n * Mod+S and Escape fire when the user has tabbed to a form button.\n */\nexport function isInputElement(element: EventTarget | null): boolean {\n if (!element) {\n return false\n }\n\n if (element instanceof HTMLInputElement) {\n const type = element.type.toLowerCase()\n if (type === 'button' || type === 'submit' || type === 'reset') {\n return false\n }\n return true\n }\n\n if (\n element instanceof HTMLTextAreaElement ||\n element instanceof HTMLSelectElement\n ) {\n return true\n }\n\n // Check for contenteditable elements (includes \"true\", \"\", \"plaintext-only\",\n // and inherited contenteditable from ancestor elements)\n if (element instanceof HTMLElement && element.isContentEditable) {\n return true\n }\n\n return false\n}\n\n/**\n * Returns the focused element for the document associated with where hotkey\n * listeners are attached (listener root). Use this instead of the global\n * `document.activeElement` so registrations scoped to an iframe (or another\n * document) read focus from the correct tree.\n */\nexport function getActiveElementForListenerTarget(\n target: HTMLElement | Document | Window,\n): Element | null {\n if (typeof document === 'undefined') {\n return null\n }\n\n // Document first (nodeType covers environments where `document instanceof Document` is false)\n if (\n (typeof Document !== 'undefined' && target instanceof Document) ||\n (typeof Node !== 'undefined' &&\n (target as Node).nodeType === Node.DOCUMENT_NODE)\n ) {\n return (target as Document).activeElement\n }\n\n if (typeof HTMLElement !== 'undefined' && target instanceof HTMLElement) {\n return target.ownerDocument.activeElement ?? null\n }\n\n // Window (global or iframe): avoid relying on `instanceof Window` alone (test DOM quirks)\n return (target as Window).document.activeElement ?? null\n}\n\n/**\n * Returns whether an event should be ignored because it originated from an\n * input-like element other than the registration target.\n *\n * This checks:\n * - the currently focused element for the listener target\n * - the event's composed path (for shadow DOM)\n * - the event target as a final fallback\n */\nexport function shouldIgnoreInputEvent(\n event: KeyboardEvent,\n listenerTarget: HTMLElement | Document | Window,\n registrationTarget: HTMLElement | Document | Window,\n): boolean {\n const focused = getActiveElementForListenerTarget(listenerTarget)\n if (focused && isInputElement(focused) && focused !== registrationTarget) {\n return true\n }\n\n if (\n event\n .composedPath()\n .some(\n (element) => isInputElement(element) && element !== registrationTarget,\n )\n ) {\n return true\n }\n\n return isInputElement(event.target) && event.target !== registrationTarget\n}\n\n/**\n * Checks if an event is for the given target (originated from or bubbled to it).\n *\n * For document/window targets, also accepts document.documentElement as currentTarget\n * to handle Brave and other browsers where currentTarget may be documentElement\n * instead of document when listeners are attached to document.\n */\nexport function isEventForTarget(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n): boolean {\n // For Document and Window, verify that our handler was indeed called for this target.\n //\n // Browser compatibility note:\n // Per the DOM spec, event.currentTarget should equal the element the listener was\n // attached to. However, some Chromium-based browsers (notably Brave) exhibit\n // non-standard behavior where event.currentTarget is set to document.documentElement\n // (<html>) instead of document when a listener is attached to document.\n // This may be related to privacy/fingerprinting protections.\n //\n // To ensure cross-browser compatibility, we accept both the expected target\n // and document.documentElement as valid currentTarget values.\n // See: https://dom.spec.whatwg.org/#dom-event-currenttarget\n if (target === document || target === window) {\n return (\n event.currentTarget === target ||\n event.currentTarget === document.documentElement\n )\n }\n\n // For Window, accept window, document, or document.documentElement (browser quirks)\n if (target === window) {\n return (\n event.currentTarget === window ||\n event.currentTarget === document ||\n event.currentTarget === document.documentElement\n )\n }\n\n // For HTMLElement, check if event originated from or bubbled to the element\n if (target instanceof HTMLElement) {\n // Check if the event's currentTarget is the target (capturing/bubbling)\n if (event.currentTarget === target) {\n return true\n }\n\n // Check if the event's target is a descendant of our target\n if (event.target instanceof Node && target.contains(event.target)) {\n return true\n }\n }\n\n return false\n}\n\n/**\n * Handles conflicts between registrations based on conflict behavior.\n *\n * @param conflictingId - The ID of the conflicting registration\n * @param keyDisplay - Display string for the conflicting key/sequence (for error messages)\n * @param conflictBehavior - How to handle the conflict\n * @param unregister - Function to unregister by ID\n */\nexport function handleConflict(\n conflictingId: string,\n keyDisplay: string,\n conflictBehavior: ConflictBehavior,\n unregister: (id: string) => void,\n): void {\n if (conflictBehavior === 'allow') {\n return\n }\n\n if (conflictBehavior === 'warn') {\n console.warn(\n `'${keyDisplay}' is already registered. Multiple handlers will be triggered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to suppress this warning.`,\n )\n return\n }\n\n if (conflictBehavior === 'error') {\n throw new Error(\n `'${keyDisplay}' is already registered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to allow multiple registrations.`,\n )\n }\n\n // At this point, conflictBehavior must be 'repl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