@tanstack/hotkeys 0.9.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.
- package/dist/_event-target.js +1 -2
- package/dist/_keyboard-event.js +1 -2
- package/dist/_match.js +1 -2
- package/dist/_named-keys.d.ts +3 -5
- package/dist/_named-keys.js +1 -2
- package/dist/_recorder-chord.js +1 -2
- package/dist/_recording-guard.js +1 -2
- package/dist/_registration.js +1 -2
- package/dist/conflicts.d.ts +4 -7
- package/dist/conflicts.js +1 -2
- package/dist/constants.d.ts +14 -17
- package/dist/constants.js +8 -9
- package/dist/display-labels.d.ts +7 -10
- package/dist/display-labels.js +1 -2
- package/dist/format.d.ts +9 -12
- package/dist/format.js +9 -7
- package/dist/hint.d.ts +3 -6
- package/dist/hint.js +1 -2
- package/dist/hotkey-manager.d.ts +8 -10
- package/dist/hotkey-manager.js +21 -3
- package/dist/hotkey-recorder.d.ts +4 -7
- package/dist/hotkey-recorder.js +9 -6
- package/dist/hotkey-sequence-recorder.d.ts +5 -8
- package/dist/hotkey-sequence-recorder.js +6 -7
- package/dist/hotkey.types.d.ts +15 -18
- package/dist/key-state-tracker.d.ts +4 -7
- package/dist/key-state-tracker.js +6 -3
- package/dist/key.types.d.ts +17 -20
- package/dist/match.d.ts +8 -11
- package/dist/match.js +1 -2
- package/dist/parse.d.ts +11 -14
- package/dist/parse.js +5 -4
- package/dist/platform.d.ts +3 -6
- package/dist/platform.js +1 -2
- package/dist/recorder-options.d.ts +6 -9
- package/dist/sequence-manager.d.ts +11 -14
- package/dist/sequence-manager.js +12 -7
- package/dist/validate.d.ts +5 -8
- package/dist/validate.js +1 -2
- package/package.json +8 -13
- package/dist/_event-target.cjs +0 -73
- package/dist/_event-target.cjs.map +0 -1
- package/dist/_event-target.js.map +0 -1
- package/dist/_keyboard-event.cjs +0 -83
- package/dist/_keyboard-event.cjs.map +0 -1
- package/dist/_keyboard-event.js.map +0 -1
- package/dist/_match.cjs +0 -49
- package/dist/_match.cjs.map +0 -1
- package/dist/_match.js.map +0 -1
- package/dist/_named-keys.cjs +0 -68
- package/dist/_named-keys.cjs.map +0 -1
- package/dist/_named-keys.d.cts +0 -8
- package/dist/_named-keys.js.map +0 -1
- package/dist/_recorder-chord.cjs +0 -44
- package/dist/_recorder-chord.cjs.map +0 -1
- package/dist/_recorder-chord.js.map +0 -1
- package/dist/_recording-guard.cjs +0 -59
- package/dist/_recording-guard.cjs.map +0 -1
- package/dist/_recording-guard.js.map +0 -1
- package/dist/_registration.cjs +0 -54
- package/dist/_registration.cjs.map +0 -1
- package/dist/_registration.js.map +0 -1
- package/dist/conflicts.cjs +0 -55
- package/dist/conflicts.cjs.map +0 -1
- package/dist/conflicts.d.cts +0 -39
- package/dist/conflicts.js.map +0 -1
- package/dist/constants.cjs +0 -410
- package/dist/constants.cjs.map +0 -1
- package/dist/constants.d.cts +0 -165
- package/dist/constants.js.map +0 -1
- package/dist/display-labels.cjs +0 -119
- package/dist/display-labels.cjs.map +0 -1
- package/dist/display-labels.d.cts +0 -93
- package/dist/display-labels.js.map +0 -1
- package/dist/format.cjs +0 -85
- package/dist/format.cjs.map +0 -1
- package/dist/format.d.cts +0 -99
- package/dist/format.js.map +0 -1
- package/dist/hint.cjs +0 -24
- package/dist/hint.cjs.map +0 -1
- package/dist/hint.d.cts +0 -23
- package/dist/hint.js.map +0 -1
- package/dist/hotkey-manager.cjs +0 -414
- package/dist/hotkey-manager.cjs.map +0 -1
- package/dist/hotkey-manager.d.cts +0 -234
- package/dist/hotkey-manager.js.map +0 -1
- package/dist/hotkey-recorder.cjs +0 -235
- package/dist/hotkey-recorder.cjs.map +0 -1
- package/dist/hotkey-recorder.d.cts +0 -119
- package/dist/hotkey-recorder.js.map +0 -1
- package/dist/hotkey-sequence-recorder.cjs +0 -255
- package/dist/hotkey-sequence-recorder.cjs.map +0 -1
- package/dist/hotkey-sequence-recorder.d.cts +0 -86
- package/dist/hotkey-sequence-recorder.js.map +0 -1
- package/dist/hotkey.types.d.cts +0 -236
- package/dist/index.cjs +0 -71
- package/dist/index.d.cts +0 -18
- package/dist/key-state-tracker.cjs +0 -197
- package/dist/key-state-tracker.cjs.map +0 -1
- package/dist/key-state-tracker.d.cts +0 -107
- package/dist/key-state-tracker.js.map +0 -1
- package/dist/key.types.d.cts +0 -82
- package/dist/match.cjs +0 -144
- package/dist/match.cjs.map +0 -1
- package/dist/match.d.cts +0 -101
- package/dist/match.js.map +0 -1
- package/dist/parse.cjs +0 -245
- package/dist/parse.cjs.map +0 -1
- package/dist/parse.d.cts +0 -134
- package/dist/parse.js.map +0 -1
- package/dist/platform.cjs +0 -55
- package/dist/platform.cjs.map +0 -1
- package/dist/platform.d.cts +0 -44
- package/dist/platform.js.map +0 -1
- package/dist/recorder-options.d.cts +0 -34
- package/dist/sequence-manager.cjs +0 -441
- package/dist/sequence-manager.cjs.map +0 -1
- package/dist/sequence-manager.d.cts +0 -172
- package/dist/sequence-manager.js.map +0 -1
- package/dist/validate.cjs +0 -112
- package/dist/validate.cjs.map +0 -1
- package/dist/validate.d.cts +0 -67
- package/dist/validate.js.map +0 -1
- package/src/_event-target.ts +0 -157
- package/src/_keyboard-event.ts +0 -101
- package/src/_match.ts +0 -96
- package/src/_named-keys.ts +0 -62
- package/src/_recorder-chord.ts +0 -76
- package/src/_recording-guard.ts +0 -63
- package/src/_registration.ts +0 -79
- package/src/conflicts.ts +0 -125
- package/src/constants.ts +0 -455
- package/src/display-labels.ts +0 -123
- package/src/format.ts +0 -201
- package/src/hint.ts +0 -42
- package/src/hotkey-manager.ts +0 -814
- package/src/hotkey-recorder.ts +0 -348
- package/src/hotkey-sequence-recorder.ts +0 -410
- package/src/hotkey.types.ts +0 -269
- package/src/index.ts +0 -17
- package/src/key-state-tracker.ts +0 -261
- package/src/key.types.ts +0 -268
- package/src/match.ts +0 -210
- package/src/parse.ts +0 -369
- package/src/platform.ts +0 -66
- package/src/recorder-options.ts +0 -34
- package/src/sequence-manager.ts +0 -769
- package/src/validate.ts +0 -179
package/dist/hotkey.types.d.cts
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import { CanonicalModifier, Key, LogicalKey } from "./key.types.cjs";
|
|
2
|
-
|
|
3
|
-
//#region src/hotkey.types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Single modifier + key combinations.
|
|
6
|
-
* Uses canonical modifiers (4) + Mod (1) = 5 modifiers.
|
|
7
|
-
*
|
|
8
|
-
* The `Mod` modifier is platform-adaptive:
|
|
9
|
-
* - **macOS**: Resolves to `Meta` (Command key ⌘)
|
|
10
|
-
* - **Windows/Linux**: Resolves to `Control` (Ctrl key)
|
|
11
|
-
*
|
|
12
|
-
* This enables cross-platform hotkey definitions that automatically adapt to the platform.
|
|
13
|
-
* For example, `Mod+S` becomes `Command+S` on Mac and `Ctrl+S` on Windows/Linux.
|
|
14
|
-
*/
|
|
15
|
-
type SingleModifierHotkey = `Control+${Key}` | `Alt+${Key}` | `Shift+${Key}` | `Meta+${Key}` | `Mod+${Key}`;
|
|
16
|
-
/**
|
|
17
|
-
* Two modifier + key combinations.
|
|
18
|
-
*
|
|
19
|
-
* **Platform-adaptive `Mod` combinations:**
|
|
20
|
-
* - `Mod+Alt` and `Mod+Shift` are included (safe on all platforms)
|
|
21
|
-
* - `Mod+Control` and `Mod+Meta` are excluded because they create duplicate modifiers:
|
|
22
|
-
* - `Mod+Control` duplicates `Control` on Windows/Linux (Mod = Control)
|
|
23
|
-
* - `Mod+Meta` duplicates `Meta` on macOS (Mod = Meta)
|
|
24
|
-
*/
|
|
25
|
-
type TwoModifierHotkey = `Control+Alt+${Key}` | `Control+Shift+${Key}` | `Control+Meta+${Key}` | `Alt+Shift+${Key}` | `Alt+Meta+${Key}` | `Shift+Meta+${Key}` | `Mod+Alt+${Key}` | `Mod+Shift+${Key}`;
|
|
26
|
-
/**
|
|
27
|
-
* Three modifier + key combinations.
|
|
28
|
-
*
|
|
29
|
-
* **Platform-adaptive `Mod` combinations:**
|
|
30
|
-
* - `Mod+Alt+Shift` is included (safe on all platforms)
|
|
31
|
-
* - `Mod+Control+Shift` and `Mod+Shift+Meta` are excluded because they create duplicate modifiers:
|
|
32
|
-
* - `Mod+Control+Shift` duplicates `Control` on Windows/Linux (Mod = Control)
|
|
33
|
-
* - `Mod+Shift+Meta` duplicates `Meta` on macOS (Mod = Meta)
|
|
34
|
-
*/
|
|
35
|
-
type ThreeModifierHotkey = `Control+Alt+Shift+${Key}` | `Control+Alt+Meta+${Key}` | `Control+Shift+Meta+${Key}` | `Alt+Shift+Meta+${Key}` | `Mod+Alt+Shift+${Key}`;
|
|
36
|
-
/**
|
|
37
|
-
* Four modifier + key combinations.
|
|
38
|
-
*
|
|
39
|
-
* Only the canonical `Control+Alt+Shift+Meta` combination is included.
|
|
40
|
-
*
|
|
41
|
-
* **Why no `Mod` combinations?**
|
|
42
|
-
* Since `Mod` resolves to either `Control` (Windows/Linux) or `Meta` (macOS), any
|
|
43
|
-
* four-modifier combination with `Mod` would create duplicate modifiers on one platform.
|
|
44
|
-
* For example:
|
|
45
|
-
* - `Mod+Control+Alt+Shift` → duplicates `Control` on Windows/Linux
|
|
46
|
-
* - `Mod+Alt+Shift+Meta` → duplicates `Meta` on macOS
|
|
47
|
-
*/
|
|
48
|
-
type FourModifierHotkey = `Control+Alt+Shift+Meta+${Key}`;
|
|
49
|
-
/**
|
|
50
|
-
* A type-safe hotkey string.
|
|
51
|
-
*
|
|
52
|
-
* Provides autocomplete for:
|
|
53
|
-
* - All single keys (letters, numbers, function keys, navigation, editing, punctuation)
|
|
54
|
-
* - Single modifier + common key (Control+S, Mod+A, Mod+/, etc.)
|
|
55
|
-
* - Two modifiers + common key (Mod+Shift+S, Control+Alt+A, etc.)
|
|
56
|
-
* - Three modifiers + common key (Control+Alt+Shift+A, Mod+Alt+Shift+S, etc.)
|
|
57
|
-
* - Four modifiers + common key (Control+Alt+Shift+Meta+A, etc.)
|
|
58
|
-
*
|
|
59
|
-
* ## Modifier Names
|
|
60
|
-
*
|
|
61
|
-
* Use canonical modifier names:
|
|
62
|
-
* - `Control` (not Ctrl) - The Control key
|
|
63
|
-
* - `Alt` (not Option) - The Alt key (Option on macOS)
|
|
64
|
-
* - `Meta` (not Command/Cmd) - The Meta/Command key (macOS only)
|
|
65
|
-
* - `Shift` - The Shift key
|
|
66
|
-
*
|
|
67
|
-
* ## Platform-Adaptive `Mod` Modifier
|
|
68
|
-
*
|
|
69
|
-
* The `Mod` modifier is a special platform-adaptive modifier that automatically resolves
|
|
70
|
-
* to the "primary modifier" on each platform:
|
|
71
|
-
*
|
|
72
|
-
* - **macOS**: `Mod` → `Meta` (Command key ⌘)
|
|
73
|
-
* - **Windows/Linux**: `Mod` → `Control` (Ctrl key)
|
|
74
|
-
*
|
|
75
|
-
* This enables cross-platform hotkey definitions that work correctly on all platforms
|
|
76
|
-
* without platform-specific code. The `Mod` modifier is resolved at runtime based on
|
|
77
|
-
* the detected platform.
|
|
78
|
-
*
|
|
79
|
-
* **When to use `Mod` vs platform-specific modifiers:**
|
|
80
|
-
* - Use `Mod` for cross-platform shortcuts (e.g., `Mod+S` for save)
|
|
81
|
-
* - Use `Meta` or `Control` when you need platform-specific behavior
|
|
82
|
-
* - Use `Mod` when you want your shortcuts to follow platform conventions automatically
|
|
83
|
-
*
|
|
84
|
-
* **Limitations:**
|
|
85
|
-
* - `Mod+Control` and `Mod+Meta` combinations are not allowed (they create duplicate
|
|
86
|
-
* modifiers on one platform)
|
|
87
|
-
* - In four-modifier combinations, only canonical modifiers are allowed (no `Mod`)
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* ```ts
|
|
91
|
-
* // Cross-platform shortcuts (recommended)
|
|
92
|
-
* const save: Hotkey = 'Mod+S' // Command+S on Mac, Ctrl+S on Windows/Linux
|
|
93
|
-
* const saveAs: Hotkey = 'Mod+Shift+S' // Command+Shift+S on Mac, Ctrl+Shift+S elsewhere
|
|
94
|
-
* const comment: Hotkey = 'Mod+/' // Command+/ on Mac, Ctrl+/ elsewhere
|
|
95
|
-
*
|
|
96
|
-
* // Platform-specific shortcuts
|
|
97
|
-
* const macOnly: Hotkey = 'Meta+S' // Command+S on Mac only
|
|
98
|
-
* const windowsOnly: Hotkey = 'Control+S' // Ctrl+S on Windows/Linux only
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
type Hotkey = Key | SingleModifierHotkey | TwoModifierHotkey | ThreeModifierHotkey | FourModifierHotkey;
|
|
102
|
-
/** Resolved modifier flags shared by both parsed binding variants. */
|
|
103
|
-
interface ParsedModifiers {
|
|
104
|
-
/** Whether Control is required. */
|
|
105
|
-
ctrl: boolean;
|
|
106
|
-
/** Whether Shift is required. */
|
|
107
|
-
shift: boolean;
|
|
108
|
-
/** Whether Alt is required. */
|
|
109
|
-
alt: boolean;
|
|
110
|
-
/** Whether Meta (Command) is required. */
|
|
111
|
-
meta: boolean;
|
|
112
|
-
/** Required modifiers in canonical order. */
|
|
113
|
-
modifiers: Array<CanonicalModifier>;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* A parsed binding matches either a logical key or a physical code, never both.
|
|
117
|
-
* Check `code !== undefined` to narrow to a physical binding. Its logical
|
|
118
|
-
* character is unknown without a keyboard event and is not stored in `key`.
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* parseHotkey('Alt+S') // { key: 'S', alt: true, ... }
|
|
122
|
-
* parseHotkey('Alt+[KeyS]') // { code: 'KeyS', alt: true, ... } — no key property
|
|
123
|
-
*/
|
|
124
|
-
type ParsedHotkey = ParsedModifiers & ({
|
|
125
|
-
key: LogicalKey | (string & {});
|
|
126
|
-
code?: never;
|
|
127
|
-
} | {
|
|
128
|
-
code: string;
|
|
129
|
-
key?: never;
|
|
130
|
-
});
|
|
131
|
-
/**
|
|
132
|
-
* A raw hotkey object for programmatic registration.
|
|
133
|
-
*
|
|
134
|
-
* Like `ParsedHotkey` but without `modifiers` (derived from booleans)
|
|
135
|
-
* and with optional modifier booleans (default to `false` when omitted).
|
|
136
|
-
* Use with `HotkeyManager.register()` and `useHotkey()` when you prefer
|
|
137
|
-
* object form over a string.
|
|
138
|
-
*
|
|
139
|
-
* The `mod` modifier is platform-adaptive: Command on macOS, Control on Windows/Linux.
|
|
140
|
-
* Pass `platform` when converting to ParsedHotkey (e.g., via `options.platform`).
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* ```ts
|
|
144
|
-
* useHotkey({ key: 'S', mod: true }, handler) // Mod+S (cross-platform)
|
|
145
|
-
* useHotkey({ key: 'S', ctrl: true }, handler) // Control+S
|
|
146
|
-
* useHotkey({ key: 'Escape' }, handler) // Escape (no modifiers)
|
|
147
|
-
* useHotkey({ key: 'A', shift: true, meta: true }, handler) // Shift+Meta+A
|
|
148
|
-
* useHotkey({ key: 'S', mod: true, shift: true }, handler) // Mod+Shift+S
|
|
149
|
-
* useHotkey({ code: 'KeyW', mod: true }, handler) // Physical Mod+KeyW
|
|
150
|
-
* useHotkey({ code: 'NumpadAdd', mod: true }, handler) // Physical numpad plus
|
|
151
|
-
* ```
|
|
152
|
-
*/
|
|
153
|
-
/** Modifier flags shared by logical-key and physical-code registrations. */
|
|
154
|
-
interface RawModifiers {
|
|
155
|
-
/** The non-modifier key (e.g., 'S', 'Escape', 'F1'). */
|
|
156
|
-
/** Platform-adaptive modifier: Command on macOS, Control on Windows/Linux. Defaults to false. */
|
|
157
|
-
mod?: boolean;
|
|
158
|
-
/** Whether the Control key is required. Defaults to false. */
|
|
159
|
-
ctrl?: boolean;
|
|
160
|
-
/** Whether the Shift key is required. Defaults to false. */
|
|
161
|
-
shift?: boolean;
|
|
162
|
-
/** Whether the Alt key is required. Defaults to false. */
|
|
163
|
-
alt?: boolean;
|
|
164
|
-
/** Whether the Meta (Command) key is required. Defaults to false. */
|
|
165
|
-
meta?: boolean;
|
|
166
|
-
}
|
|
167
|
-
type RawHotkey = ({
|
|
168
|
-
key: LogicalKey | (string & {});
|
|
169
|
-
code?: never;
|
|
170
|
-
} & RawModifiers) | ({
|
|
171
|
-
code: string;
|
|
172
|
-
key?: never;
|
|
173
|
-
} & RawModifiers);
|
|
174
|
-
/**
|
|
175
|
-
* A hotkey that can be passed to `HotkeyManager.register()` and `useHotkey()`.
|
|
176
|
-
* Either a type-safe string (`Hotkey`) or a raw object (`RawHotkey`).
|
|
177
|
-
*/
|
|
178
|
-
type RegisterableHotkey = Hotkey | RawHotkey;
|
|
179
|
-
/**
|
|
180
|
-
* Context passed to hotkey callbacks along with the keyboard event.
|
|
181
|
-
*/
|
|
182
|
-
interface HotkeyCallbackContext {
|
|
183
|
-
/** The original hotkey string that was registered */
|
|
184
|
-
hotkey: Hotkey;
|
|
185
|
-
/** The parsed representation of the hotkey */
|
|
186
|
-
parsedHotkey: ParsedHotkey;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Callback function type for hotkey handlers.
|
|
190
|
-
*
|
|
191
|
-
* @param event - The keyboard event that triggered the hotkey
|
|
192
|
-
* @param context - Additional context including the hotkey and parsed hotkey
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```ts
|
|
196
|
-
* const handler: HotkeyCallback = (event, { hotkey, parsedHotkey }) => {
|
|
197
|
-
* console.log(`Hotkey ${hotkey} was pressed`)
|
|
198
|
-
* console.log(`Modifiers:`, parsedHotkey.modifiers)
|
|
199
|
-
* }
|
|
200
|
-
* ```
|
|
201
|
-
*/
|
|
202
|
-
type HotkeyCallback = (event: KeyboardEvent, context: HotkeyCallbackContext) => void;
|
|
203
|
-
/**
|
|
204
|
-
* Metadata for hotkey and sequence registrations.
|
|
205
|
-
* Includes `name`, `description`, and `group` by default. Extend via declaration merging:
|
|
206
|
-
*
|
|
207
|
-
* @example
|
|
208
|
-
* ```ts
|
|
209
|
-
* declare module '@tanstack/hotkeys' {
|
|
210
|
-
* interface HotkeyMeta {
|
|
211
|
-
* category?: string
|
|
212
|
-
* icon?: string
|
|
213
|
-
* }
|
|
214
|
-
* }
|
|
215
|
-
* ```
|
|
216
|
-
*/
|
|
217
|
-
interface HotkeyMeta {
|
|
218
|
-
/** Human-readable name for this hotkey (e.g., "Save Document") */
|
|
219
|
-
name?: string;
|
|
220
|
-
/** Description of what this hotkey does */
|
|
221
|
-
description?: string;
|
|
222
|
-
/** Optional display group. Does not affect matching or scope. */
|
|
223
|
-
group?: string;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Behavior when registering a hotkey/sequence that conflicts with an existing registration.
|
|
227
|
-
*
|
|
228
|
-
* - `'warn'` - Log a warning to the console but allow both registrations (default)
|
|
229
|
-
* - `'error'` - Throw an error and prevent the new registration
|
|
230
|
-
* - `'replace'` - Unregister the existing registration and register the new one
|
|
231
|
-
* - `'allow'` - Allow multiple registrations without warning
|
|
232
|
-
*/
|
|
233
|
-
type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow';
|
|
234
|
-
//#endregion
|
|
235
|
-
export { ConflictBehavior, FourModifierHotkey, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyMeta, ParsedHotkey, ParsedModifiers, RawHotkey, RawModifiers, RegisterableHotkey, SingleModifierHotkey, ThreeModifierHotkey, TwoModifierHotkey };
|
|
236
|
-
//# sourceMappingURL=hotkey.types.d.cts.map
|
package/dist/index.cjs
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_constants = require('./constants.cjs');
|
|
3
|
-
const require_display_labels = require('./display-labels.cjs');
|
|
4
|
-
const require_platform = require('./platform.cjs');
|
|
5
|
-
const require_parse = require('./parse.cjs');
|
|
6
|
-
const require_format = require('./format.cjs');
|
|
7
|
-
const require_validate = require('./validate.cjs');
|
|
8
|
-
const require_match = require('./match.cjs');
|
|
9
|
-
const require_hint = require('./hint.cjs');
|
|
10
|
-
const require_hotkey_manager = require('./hotkey-manager.cjs');
|
|
11
|
-
const require_sequence_manager = require('./sequence-manager.cjs');
|
|
12
|
-
const require_conflicts = require('./conflicts.cjs');
|
|
13
|
-
const require_key_state_tracker = require('./key-state-tracker.cjs');
|
|
14
|
-
const require_hotkey_recorder = require('./hotkey-recorder.cjs');
|
|
15
|
-
const require_hotkey_sequence_recorder = require('./hotkey-sequence-recorder.cjs');
|
|
16
|
-
|
|
17
|
-
exports.ALL_KEYS = require_constants.ALL_KEYS;
|
|
18
|
-
exports.DEFAULT_SEQUENCE_TIMEOUT = require_sequence_manager.DEFAULT_SEQUENCE_TIMEOUT;
|
|
19
|
-
exports.EDITING_KEYS = require_constants.EDITING_KEYS;
|
|
20
|
-
exports.FUNCTION_KEYS = require_constants.FUNCTION_KEYS;
|
|
21
|
-
exports.HotkeyManager = require_hotkey_manager.HotkeyManager;
|
|
22
|
-
exports.HotkeyRecorder = require_hotkey_recorder.HotkeyRecorder;
|
|
23
|
-
exports.HotkeySequenceRecorder = require_hotkey_sequence_recorder.HotkeySequenceRecorder;
|
|
24
|
-
exports.KEY_DISPLAY_SYMBOLS = require_display_labels.KEY_DISPLAY_SYMBOLS;
|
|
25
|
-
exports.KeyStateTracker = require_key_state_tracker.KeyStateTracker;
|
|
26
|
-
exports.LETTER_KEYS = require_constants.LETTER_KEYS;
|
|
27
|
-
exports.LINUX_MODIFIER_LABELS = require_display_labels.LINUX_MODIFIER_LABELS;
|
|
28
|
-
exports.MAC_MODIFIER_LABELS = require_display_labels.MAC_MODIFIER_LABELS;
|
|
29
|
-
exports.MAC_MODIFIER_SYMBOLS = require_display_labels.MAC_MODIFIER_SYMBOLS;
|
|
30
|
-
exports.MODIFIER_ALIASES = require_constants.MODIFIER_ALIASES;
|
|
31
|
-
exports.MODIFIER_KEYS = require_constants.MODIFIER_KEYS;
|
|
32
|
-
exports.MODIFIER_ORDER = require_constants.MODIFIER_ORDER;
|
|
33
|
-
exports.NAVIGATION_KEYS = require_constants.NAVIGATION_KEYS;
|
|
34
|
-
exports.NUMBER_KEYS = require_constants.NUMBER_KEYS;
|
|
35
|
-
exports.PUNCTUATION_CODE_MAP = require_constants.PUNCTUATION_CODE_MAP;
|
|
36
|
-
exports.PUNCTUATION_KEYS = require_constants.PUNCTUATION_KEYS;
|
|
37
|
-
exports.PUNCTUATION_KEY_DISPLAY_LABELS = require_display_labels.PUNCTUATION_KEY_DISPLAY_LABELS;
|
|
38
|
-
exports.SequenceManager = require_sequence_manager.SequenceManager;
|
|
39
|
-
exports.WINDOWS_MODIFIER_LABELS = require_display_labels.WINDOWS_MODIFIER_LABELS;
|
|
40
|
-
exports.areHotkeysEqual = require_match.areHotkeysEqual;
|
|
41
|
-
exports.assertValidHotkey = require_validate.assertValidHotkey;
|
|
42
|
-
exports.checkHotkey = require_validate.checkHotkey;
|
|
43
|
-
exports.createHotkeyHandler = require_match.createHotkeyHandler;
|
|
44
|
-
exports.createMultiHotkeyHandler = require_match.createMultiHotkeyHandler;
|
|
45
|
-
exports.createSequenceMatcher = require_sequence_manager.createSequenceMatcher;
|
|
46
|
-
exports.detectPlatform = require_platform.detectPlatform;
|
|
47
|
-
exports.findHotkeyConflicts = require_conflicts.findHotkeyConflicts;
|
|
48
|
-
exports.formatForDisplay = require_format.formatForDisplay;
|
|
49
|
-
exports.formatHotkey = require_format.formatHotkey;
|
|
50
|
-
exports.formatHotkeySequence = require_format.formatHotkeySequence;
|
|
51
|
-
exports.formatWithLabels = require_format.formatWithLabels;
|
|
52
|
-
exports.getHotkeyManager = require_hotkey_manager.getHotkeyManager;
|
|
53
|
-
exports.getKeyStateTracker = require_key_state_tracker.getKeyStateTracker;
|
|
54
|
-
exports.getSequenceManager = require_sequence_manager.getSequenceManager;
|
|
55
|
-
exports.hasNonModifierKey = require_parse.hasNonModifierKey;
|
|
56
|
-
exports.isModifierKey = require_parse.isModifierKey;
|
|
57
|
-
exports.isSingleLetterKey = require_constants.isSingleLetterKey;
|
|
58
|
-
exports.matchesHeldModifiers = require_hint.matchesHeldModifiers;
|
|
59
|
-
exports.matchesKeyboardEvent = require_match.matchesKeyboardEvent;
|
|
60
|
-
exports.normalizeHotkey = require_parse.normalizeHotkey;
|
|
61
|
-
exports.normalizeHotkeyFromEvent = require_parse.normalizeHotkeyFromEvent;
|
|
62
|
-
exports.normalizeHotkeyFromParsed = require_parse.normalizeHotkeyFromParsed;
|
|
63
|
-
exports.normalizeKeyName = require_constants.normalizeKeyName;
|
|
64
|
-
exports.normalizeRegisterableHotkey = require_parse.normalizeRegisterableHotkey;
|
|
65
|
-
exports.parseHotkey = require_parse.parseHotkey;
|
|
66
|
-
exports.parseKeyboardEvent = require_parse.parseKeyboardEvent;
|
|
67
|
-
exports.parseRegisterableHotkey = require_parse.parseRegisterableHotkey;
|
|
68
|
-
exports.rawHotkeyToParsedHotkey = require_parse.rawHotkeyToParsedHotkey;
|
|
69
|
-
exports.resolveModifier = require_platform.resolveModifier;
|
|
70
|
-
exports.toHotkeyRegistrationView = require_hotkey_manager.toHotkeyRegistrationView;
|
|
71
|
-
exports.validateHotkey = require_validate.validateHotkey;
|
package/dist/index.d.cts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { CanonicalModifier, EditingKey, FunctionKey, IndividualKey, Key, LetterKey, LogicalKey, Modifier, NamedKey, NavigationKey, NonPunctuationKey, NormalizedKeyboardEvent, NumberKey, PhysicalKey, PhysicalKeyCode, PunctuationKey } from "./key.types.cjs";
|
|
2
|
-
import { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, LETTER_KEYS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, isSingleLetterKey, normalizeKeyName } from "./constants.cjs";
|
|
3
|
-
import { KEY_DISPLAY_SYMBOLS, LINUX_MODIFIER_LABELS, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, PUNCTUATION_KEY_DISPLAY_LABELS, WINDOWS_MODIFIER_LABELS } from "./display-labels.cjs";
|
|
4
|
-
import { detectPlatform, resolveModifier } from "./platform.cjs";
|
|
5
|
-
import { ConflictBehavior, FourModifierHotkey, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyMeta, ParsedHotkey, ParsedModifiers, RawHotkey, RawModifiers, RegisterableHotkey, SingleModifierHotkey, ThreeModifierHotkey, TwoModifierHotkey } from "./hotkey.types.cjs";
|
|
6
|
-
import { DisplayHotkey, FormatDisplayOptions, formatForDisplay, formatHotkey, formatHotkeySequence, formatWithLabels } from "./format.cjs";
|
|
7
|
-
import { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, parseRegisterableHotkey, rawHotkeyToParsedHotkey } from "./parse.cjs";
|
|
8
|
-
import { ValidationResult, assertValidHotkey, checkHotkey, validateHotkey } from "./validate.cjs";
|
|
9
|
-
import { CreateHotkeyHandlerOptions, KeyboardEventMatch, MultiHotkeyHandler, areHotkeysEqual, createHotkeyHandler, createMultiHotkeyHandler, matchesKeyboardEvent } from "./match.cjs";
|
|
10
|
-
import { HeldModifierOptions, matchesHeldModifiers } from "./hint.cjs";
|
|
11
|
-
import { HotkeyManager, HotkeyOptions, HotkeyRegistration, HotkeyRegistrationHandle, HotkeyRegistrationView, getHotkeyManager, toHotkeyRegistrationView } from "./hotkey-manager.cjs";
|
|
12
|
-
import { DEFAULT_SEQUENCE_TIMEOUT, HotkeySequence, SequenceManager, SequenceOptions, SequenceRegistration, SequenceRegistrationHandle, SequenceRegistrationView, Target, createSequenceMatcher, getSequenceManager } from "./sequence-manager.cjs";
|
|
13
|
-
import { HotkeyConflict, HotkeyConflictOptions, findHotkeyConflicts } from "./conflicts.cjs";
|
|
14
|
-
import { KeyStateTracker, KeyStateTrackerState, getKeyStateTracker } from "./key-state-tracker.cjs";
|
|
15
|
-
import { HotkeyRecorderValidationContext, HotkeySequenceRecorderValidationContext, RecorderKeyMode, RecorderOptions, RecorderRejection } from "./recorder-options.cjs";
|
|
16
|
-
import { HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState } from "./hotkey-recorder.cjs";
|
|
17
|
-
import { HotkeySequenceRecorder, HotkeySequenceRecorderCommitKeys, HotkeySequenceRecorderOptions, HotkeySequenceRecorderState } from "./hotkey-sequence-recorder.cjs";
|
|
18
|
-
export { ALL_KEYS, CanonicalModifier, ConflictBehavior, CreateHotkeyHandlerOptions, DEFAULT_SEQUENCE_TIMEOUT, DisplayHotkey, EDITING_KEYS, EditingKey, FUNCTION_KEYS, FormatDisplayOptions, FourModifierHotkey, FunctionKey, HeldModifierOptions, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyConflict, HotkeyConflictOptions, HotkeyManager, HotkeyMeta, HotkeyOptions, HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState, HotkeyRecorderValidationContext, HotkeyRegistration, HotkeyRegistrationHandle, HotkeyRegistrationView, HotkeySequence, HotkeySequenceRecorder, HotkeySequenceRecorderCommitKeys, HotkeySequenceRecorderOptions, HotkeySequenceRecorderState, HotkeySequenceRecorderValidationContext, IndividualKey, KEY_DISPLAY_SYMBOLS, Key, KeyStateTracker, KeyStateTrackerState, KeyboardEventMatch, LETTER_KEYS, LINUX_MODIFIER_LABELS, LetterKey, LogicalKey, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, Modifier, MultiHotkeyHandler, NAVIGATION_KEYS, NUMBER_KEYS, NamedKey, NavigationKey, NonPunctuationKey, NormalizedKeyboardEvent, NumberKey, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, PUNCTUATION_KEY_DISPLAY_LABELS, ParsedHotkey, ParsedModifiers, PhysicalKey, PhysicalKeyCode, PunctuationKey, RawHotkey, RawModifiers, RecorderKeyMode, RecorderOptions, RecorderRejection, RegisterableHotkey, SequenceManager, SequenceOptions, SequenceRegistration, SequenceRegistrationHandle, SequenceRegistrationView, SingleModifierHotkey, Target, ThreeModifierHotkey, TwoModifierHotkey, ValidationResult, WINDOWS_MODIFIER_LABELS, areHotkeysEqual, assertValidHotkey, checkHotkey, createHotkeyHandler, createMultiHotkeyHandler, createSequenceMatcher, detectPlatform, findHotkeyConflicts, formatForDisplay, formatHotkey, formatHotkeySequence, formatWithLabels, getHotkeyManager, getKeyStateTracker, getSequenceManager, hasNonModifierKey, isModifierKey, isSingleLetterKey, matchesHeldModifiers, matchesKeyboardEvent, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeKeyName, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, parseRegisterableHotkey, rawHotkeyToParsedHotkey, resolveModifier, toHotkeyRegistrationView, validateHotkey };
|
|
@@ -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
|
-
#heldEntries = /* @__PURE__ */ new Map();
|
|
51
|
-
#keydownListener = null;
|
|
52
|
-
#keyupListener = null;
|
|
53
|
-
#blurListener = null;
|
|
54
|
-
constructor() {
|
|
55
|
-
this.store = new _tanstack_store.Store(getDefaultKeyStateTrackerState());
|
|
56
|
-
this.#setupListeners();
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Gets the singleton instance of KeyStateTracker.
|
|
60
|
-
*/
|
|
61
|
-
static getInstance() {
|
|
62
|
-
if (!KeyStateTracker.#instance) KeyStateTracker.#instance = new KeyStateTracker();
|
|
63
|
-
return KeyStateTracker.#instance;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Resets the singleton instance. Useful for testing.
|
|
67
|
-
*/
|
|
68
|
-
static resetInstance() {
|
|
69
|
-
if (KeyStateTracker.#instance) {
|
|
70
|
-
KeyStateTracker.#instance.destroy();
|
|
71
|
-
KeyStateTracker.#instance = null;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Sets up the keyboard event listeners.
|
|
76
|
-
*/
|
|
77
|
-
#setupListeners() {
|
|
78
|
-
if (typeof document === "undefined") return;
|
|
79
|
-
this.#keydownListener = (event) => {
|
|
80
|
-
const key = require_constants.normalizeKeyName(event.key);
|
|
81
|
-
const identity = event.code || `key:${key}`;
|
|
82
|
-
if (!this.#heldEntries.has(identity)) {
|
|
83
|
-
this.#heldEntries.set(identity, {
|
|
84
|
-
key,
|
|
85
|
-
code: event.code
|
|
86
|
-
});
|
|
87
|
-
this.#syncState();
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
this.#keyupListener = (event) => {
|
|
91
|
-
const key = require_constants.normalizeKeyName(event.key);
|
|
92
|
-
const identity = event.code || `key:${key}`;
|
|
93
|
-
this.#heldEntries.delete(identity);
|
|
94
|
-
if (!event.code) {
|
|
95
|
-
for (const [entryIdentity, entry] of this.#heldEntries) if (entry.key === key) this.#heldEntries.delete(entryIdentity);
|
|
96
|
-
}
|
|
97
|
-
if (require_constants.MODIFIER_KEYS.has(key)) {
|
|
98
|
-
for (const [entryIdentity, entry] of this.#heldEntries) if (!require_constants.MODIFIER_KEYS.has(entry.key)) this.#heldEntries.delete(entryIdentity);
|
|
99
|
-
}
|
|
100
|
-
this.#syncState();
|
|
101
|
-
};
|
|
102
|
-
this.#blurListener = () => {
|
|
103
|
-
if (this.#heldEntries.size > 0) {
|
|
104
|
-
this.#heldEntries.clear();
|
|
105
|
-
this.#syncState();
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
document.addEventListener("keydown", this.#keydownListener, true);
|
|
109
|
-
document.addEventListener("keyup", this.#keyupListener, true);
|
|
110
|
-
window.addEventListener("blur", this.#blurListener);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Syncs the internal Set to the Store state.
|
|
114
|
-
*/
|
|
115
|
-
#syncState() {
|
|
116
|
-
const heldKeys = Array.from(new Set(Array.from(this.#heldEntries.values(), (entry) => entry.key)));
|
|
117
|
-
const heldCodes = Object.fromEntries(Array.from(this.#heldEntries.values(), (entry) => [entry.key, entry.code]));
|
|
118
|
-
this.store.setState(() => ({
|
|
119
|
-
heldKeys,
|
|
120
|
-
heldCodes
|
|
121
|
-
}));
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Removes the keyboard event listeners.
|
|
125
|
-
*/
|
|
126
|
-
#removeListeners() {
|
|
127
|
-
if (typeof document === "undefined") return;
|
|
128
|
-
if (this.#keydownListener) {
|
|
129
|
-
document.removeEventListener("keydown", this.#keydownListener, true);
|
|
130
|
-
this.#keydownListener = null;
|
|
131
|
-
}
|
|
132
|
-
if (this.#keyupListener) {
|
|
133
|
-
document.removeEventListener("keyup", this.#keyupListener, true);
|
|
134
|
-
this.#keyupListener = null;
|
|
135
|
-
}
|
|
136
|
-
if (this.#blurListener) {
|
|
137
|
-
window.removeEventListener("blur", this.#blurListener);
|
|
138
|
-
this.#blurListener = null;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Gets an array of currently held key names.
|
|
143
|
-
*
|
|
144
|
-
* @returns Array of key names currently being pressed
|
|
145
|
-
*/
|
|
146
|
-
getHeldKeys() {
|
|
147
|
-
return this.store.state.heldKeys;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Checks if a specific key is currently being held.
|
|
151
|
-
*
|
|
152
|
-
* @param key - The key name to check (case-insensitive)
|
|
153
|
-
* @returns True if the key is currently held
|
|
154
|
-
*/
|
|
155
|
-
isKeyHeld(key) {
|
|
156
|
-
const normalizedKey = require_constants.normalizeKeyName(key);
|
|
157
|
-
return Array.from(this.#heldEntries.values()).some((entry) => entry.key === normalizedKey);
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Checks if any of the given keys are currently held.
|
|
161
|
-
*
|
|
162
|
-
* @param keys - Array of key names to check
|
|
163
|
-
* @returns True if any of the keys are currently held
|
|
164
|
-
*/
|
|
165
|
-
isAnyKeyHeld(keys) {
|
|
166
|
-
return keys.some((key) => this.isKeyHeld(key));
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Checks if all of the given keys are currently held.
|
|
170
|
-
*
|
|
171
|
-
* @param keys - Array of key names to check
|
|
172
|
-
* @returns True if all of the keys are currently held
|
|
173
|
-
*/
|
|
174
|
-
areAllKeysHeld(keys) {
|
|
175
|
-
return keys.every((key) => this.isKeyHeld(key));
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Destroys the tracker and removes all listeners.
|
|
179
|
-
*/
|
|
180
|
-
destroy() {
|
|
181
|
-
this.#removeListeners();
|
|
182
|
-
this.#heldEntries.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","#heldEntries","#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 #heldEntries: Map<string, { key: string; code: 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 const identity = event.code || `key:${key}`\n if (!this.#heldEntries.has(identity)) {\n this.#heldEntries.set(identity, { key, code: event.code })\n this.#syncState()\n }\n }\n\n this.#keyupListener = (event: KeyboardEvent) => {\n const key = normalizeKeyName(event.key)\n const identity = event.code || `key:${key}`\n this.#heldEntries.delete(identity)\n\n // Code-less keyup events can only be paired by their logical key.\n if (!event.code) {\n for (const [entryIdentity, entry] of this.#heldEntries) {\n if (entry.key === key) this.#heldEntries.delete(entryIdentity)\n }\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 [entryIdentity, entry] of this.#heldEntries) {\n if (!MODIFIER_KEYS.has(entry.key)) {\n this.#heldEntries.delete(entryIdentity)\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.#heldEntries.size > 0) {\n this.#heldEntries.clear()\n this.#syncState()\n }\n }\n\n document.addEventListener('keydown', this.#keydownListener, true)\n document.addEventListener('keyup', this.#keyupListener, true)\n window.addEventListener('blur', this.#blurListener)\n }\n\n /**\n * Syncs the internal Set to the Store state.\n */\n #syncState(): void {\n const heldKeys = Array.from(\n new Set(Array.from(this.#heldEntries.values(), (entry) => entry.key)),\n )\n const heldCodes = Object.fromEntries(\n Array.from(this.#heldEntries.values(), (entry) => [\n entry.key,\n entry.code,\n ]),\n )\n this.store.setState(() => ({\n heldKeys,\n heldCodes,\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, true)\n this.#keydownListener = null\n }\n\n if (this.#keyupListener) {\n document.removeEventListener('keyup', this.#keyupListener, true)\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 Array.from(this.#heldEntries.values()).some(\n (entry) => entry.key === normalizedKey,\n )\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.#heldEntries.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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"}
|
|
@@ -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
|