@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.
- package/LICENSE +21 -0
- package/README.md +121 -45
- package/dist/constants.cjs +444 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +226 -0
- package/dist/constants.d.ts +226 -0
- package/dist/constants.js +428 -0
- package/dist/constants.js.map +1 -0
- package/dist/format.cjs +178 -0
- package/dist/format.cjs.map +1 -0
- package/dist/format.d.cts +110 -0
- package/dist/format.d.ts +110 -0
- package/dist/format.js +175 -0
- package/dist/format.js.map +1 -0
- package/dist/hotkey-manager.cjs +420 -0
- package/dist/hotkey-manager.cjs.map +1 -0
- package/dist/hotkey-manager.d.cts +207 -0
- package/dist/hotkey-manager.d.ts +207 -0
- package/dist/hotkey-manager.js +419 -0
- package/dist/hotkey-manager.js.map +1 -0
- package/dist/hotkey.d.cts +278 -0
- package/dist/hotkey.d.ts +278 -0
- package/dist/index.cjs +54 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/key-state-tracker.cjs +197 -0
- package/dist/key-state-tracker.cjs.map +1 -0
- package/dist/key-state-tracker.d.cts +107 -0
- package/dist/key-state-tracker.d.ts +107 -0
- package/dist/key-state-tracker.js +196 -0
- package/dist/key-state-tracker.js.map +1 -0
- package/dist/match.cjs +143 -0
- package/dist/match.cjs.map +1 -0
- package/dist/match.d.cts +79 -0
- package/dist/match.d.ts +79 -0
- package/dist/match.js +141 -0
- package/dist/match.js.map +1 -0
- package/dist/parse.cjs +266 -0
- package/dist/parse.cjs.map +1 -0
- package/dist/parse.d.cts +169 -0
- package/dist/parse.d.ts +169 -0
- package/dist/parse.js +258 -0
- package/dist/parse.js.map +1 -0
- package/dist/recorder.cjs +177 -0
- package/dist/recorder.cjs.map +1 -0
- package/dist/recorder.d.cts +108 -0
- package/dist/recorder.d.ts +108 -0
- package/dist/recorder.js +177 -0
- package/dist/recorder.js.map +1 -0
- package/dist/sequence.cjs +242 -0
- package/dist/sequence.cjs.map +1 -0
- package/dist/sequence.d.cts +109 -0
- package/dist/sequence.d.ts +109 -0
- package/dist/sequence.js +240 -0
- package/dist/sequence.js.map +1 -0
- package/dist/validate.cjs +116 -0
- package/dist/validate.cjs.map +1 -0
- package/dist/validate.d.cts +56 -0
- package/dist/validate.d.ts +56 -0
- package/dist/validate.js +114 -0
- package/dist/validate.js.map +1 -0
- package/package.json +55 -7
- package/src/constants.ts +514 -0
- package/src/format.ts +261 -0
- package/src/hotkey-manager.ts +822 -0
- package/src/hotkey.ts +411 -0
- package/src/index.ts +10 -0
- package/src/key-state-tracker.ts +249 -0
- package/src/match.ts +222 -0
- package/src/parse.ts +368 -0
- package/src/recorder.ts +266 -0
- package/src/sequence.ts +391 -0
- package/src/validate.ts +171 -0
|
@@ -0,0 +1,197 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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":"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"}
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
|
@@ -0,0 +1,107 @@
|
|
|
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.ts.map
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { MODIFIER_KEYS, normalizeKeyName } from "./constants.js";
|
|
2
|
+
import { Store } from "@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 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 = 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 = normalizeKeyName(event.key);
|
|
90
|
+
if (this.#heldKeysSet.has(key)) {
|
|
91
|
+
this.#heldKeysSet.delete(key);
|
|
92
|
+
this.#heldCodesMap.delete(key);
|
|
93
|
+
}
|
|
94
|
+
if (MODIFIER_KEYS.has(key)) {
|
|
95
|
+
for (const heldKey of this.#heldKeysSet) if (!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 = 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
|
+
export { KeyStateTracker, getKeyStateTracker };
|
|
196
|
+
//# sourceMappingURL=key-state-tracker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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":"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"}
|