@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
package/dist/parse.cjs
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/parse.ts
|
|
4
|
+
/**
|
|
5
|
+
* Parses a hotkey string into its component parts.
|
|
6
|
+
*
|
|
7
|
+
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
8
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
9
|
+
* @returns A ParsedHotkey object with the key and modifier flags
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
14
|
+
* parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }
|
|
15
|
+
* parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function parseHotkey(hotkey, platform = require_constants.detectPlatform()) {
|
|
19
|
+
const parts = hotkey.split("+");
|
|
20
|
+
const modifiers = /* @__PURE__ */ new Set();
|
|
21
|
+
let key = "";
|
|
22
|
+
for (let i = 0; i < parts.length; i++) {
|
|
23
|
+
const part = parts[i].trim();
|
|
24
|
+
if (i === parts.length - 1) key = require_constants.normalizeKeyName(part);
|
|
25
|
+
else {
|
|
26
|
+
const alias = require_constants.MODIFIER_ALIASES[part] ?? require_constants.MODIFIER_ALIASES[part.toLowerCase()];
|
|
27
|
+
if (alias) {
|
|
28
|
+
const resolved = require_constants.resolveModifier(alias, platform);
|
|
29
|
+
modifiers.add(resolved);
|
|
30
|
+
} else if (parts.length === 1) key = require_constants.normalizeKeyName(part);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (!key && parts.length > 0) key = require_constants.normalizeKeyName(parts[parts.length - 1].trim());
|
|
34
|
+
return {
|
|
35
|
+
key,
|
|
36
|
+
ctrl: modifiers.has("Control"),
|
|
37
|
+
shift: modifiers.has("Shift"),
|
|
38
|
+
alt: modifiers.has("Alt"),
|
|
39
|
+
meta: modifiers.has("Meta"),
|
|
40
|
+
modifiers: require_constants.MODIFIER_ORDER.filter((m) => modifiers.has(m))
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Converts a RawHotkey object to a ParsedHotkey.
|
|
45
|
+
* Optional modifier booleans default to false; modifiers array is derived from them.
|
|
46
|
+
* When `mod` is true, it is resolved to Control or Meta based on platform.
|
|
47
|
+
*
|
|
48
|
+
* @param raw - The raw hotkey object
|
|
49
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
50
|
+
* @returns A ParsedHotkey suitable for matching and formatting
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* rawHotkeyToParsedHotkey({ key: 'Escape' })
|
|
55
|
+
* // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }
|
|
56
|
+
*
|
|
57
|
+
* rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')
|
|
58
|
+
* // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
59
|
+
*
|
|
60
|
+
* rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')
|
|
61
|
+
* // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function rawHotkeyToParsedHotkey(raw, platform = require_constants.detectPlatform()) {
|
|
65
|
+
let ctrl = raw.ctrl ?? false;
|
|
66
|
+
const shift = raw.shift ?? false;
|
|
67
|
+
const alt = raw.alt ?? false;
|
|
68
|
+
let meta = raw.meta ?? false;
|
|
69
|
+
if (raw.mod) if (require_constants.resolveModifier("Mod", platform) === "Control") ctrl = true;
|
|
70
|
+
else meta = true;
|
|
71
|
+
const modifiers = require_constants.MODIFIER_ORDER.filter((m) => {
|
|
72
|
+
switch (m) {
|
|
73
|
+
case "Control": return ctrl;
|
|
74
|
+
case "Shift": return shift;
|
|
75
|
+
case "Alt": return alt;
|
|
76
|
+
case "Meta": return meta;
|
|
77
|
+
default: return false;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return {
|
|
81
|
+
key: raw.key,
|
|
82
|
+
ctrl,
|
|
83
|
+
shift,
|
|
84
|
+
alt,
|
|
85
|
+
meta,
|
|
86
|
+
modifiers
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Normalizes a hotkey string to its canonical form.
|
|
91
|
+
*
|
|
92
|
+
* The canonical form uses:
|
|
93
|
+
* - Full modifier names (Control, Alt, Shift, Meta)
|
|
94
|
+
* - Modifiers in order: Control+Alt+Shift+Meta
|
|
95
|
+
* - Uppercase letters for single-character keys
|
|
96
|
+
* - Proper casing for special keys (Escape, not escape)
|
|
97
|
+
*
|
|
98
|
+
* @param hotkey - The hotkey string to normalize
|
|
99
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
100
|
+
* @returns The normalized hotkey string
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'
|
|
105
|
+
* normalizeHotkey('ctrl+a') // 'Control+A'
|
|
106
|
+
* normalizeHotkey('esc') // 'Escape'
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
function normalizeHotkey(hotkey, platform = require_constants.detectPlatform()) {
|
|
110
|
+
const parsed = parseHotkey(hotkey, platform);
|
|
111
|
+
const parts = [];
|
|
112
|
+
for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
113
|
+
parts.push(parsed.key);
|
|
114
|
+
return parts.join("+");
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Checks if a string represents a modifier key.
|
|
118
|
+
*
|
|
119
|
+
* @param key - The string to check
|
|
120
|
+
* @returns True if the string is a recognized modifier
|
|
121
|
+
*/
|
|
122
|
+
function isModifier(key) {
|
|
123
|
+
return key in require_constants.MODIFIER_ALIASES || key.toLowerCase() in require_constants.MODIFIER_ALIASES;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Parses a KeyboardEvent into a ParsedHotkey object.
|
|
127
|
+
*
|
|
128
|
+
* This function extracts the key and modifier state from a keyboard event
|
|
129
|
+
* and converts it into the same format used by `parseHotkey()`.
|
|
130
|
+
*
|
|
131
|
+
* @param event - The KeyboardEvent to parse
|
|
132
|
+
* @param platform - The target platform for resolving modifiers (defaults to auto-detection)
|
|
133
|
+
* @returns A ParsedHotkey object representing the keyboard event
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* document.addEventListener('keydown', (event) => {
|
|
138
|
+
* const parsed = parseKeyboardEvent(event)
|
|
139
|
+
* console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }
|
|
140
|
+
* })
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
function parseKeyboardEvent(event) {
|
|
144
|
+
const normalizedKey = require_constants.normalizeKeyName(event.key);
|
|
145
|
+
const modifiers = [];
|
|
146
|
+
if (event.ctrlKey) modifiers.push("Control");
|
|
147
|
+
if (event.altKey) modifiers.push("Alt");
|
|
148
|
+
if (event.shiftKey) modifiers.push("Shift");
|
|
149
|
+
if (event.metaKey) modifiers.push("Meta");
|
|
150
|
+
return {
|
|
151
|
+
key: normalizedKey,
|
|
152
|
+
ctrl: event.ctrlKey,
|
|
153
|
+
shift: event.shiftKey,
|
|
154
|
+
alt: event.altKey,
|
|
155
|
+
meta: event.metaKey,
|
|
156
|
+
modifiers
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Converts a KeyboardEvent directly to a hotkey string.
|
|
161
|
+
*
|
|
162
|
+
* This is a convenience function that combines `parseKeyboardEvent()` and formatting.
|
|
163
|
+
* The resulting hotkey string uses canonical modifier names (Control, Alt, Shift, Meta)
|
|
164
|
+
* and is suitable for use with `useHotkey()` and other hotkey functions.
|
|
165
|
+
*
|
|
166
|
+
* @param event - The KeyboardEvent to convert
|
|
167
|
+
* @param platform - The target platform (defaults to auto-detection)
|
|
168
|
+
* @returns A hotkey string in canonical form (e.g., 'Control+Shift+S')
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* document.addEventListener('keydown', (event) => {
|
|
173
|
+
* const hotkey = keyboardEventToHotkey(event)
|
|
174
|
+
* console.log(hotkey) // 'Control+Shift+S'
|
|
175
|
+
* useHotkey(hotkey, () => console.log('Shortcut triggered'))
|
|
176
|
+
* })
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
function keyboardEventToHotkey(event) {
|
|
180
|
+
const parsed = parseKeyboardEvent(event);
|
|
181
|
+
const parts = [];
|
|
182
|
+
for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
183
|
+
parts.push(parsed.key);
|
|
184
|
+
return parts.join("+");
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Checks if a KeyboardEvent represents a modifier-only key press.
|
|
188
|
+
*
|
|
189
|
+
* Modifier-only keys are keys like 'Control', 'Shift', 'Alt', 'Meta', etc.
|
|
190
|
+
* that don't have an associated character or action key. This is useful
|
|
191
|
+
* for filtering out modifier key presses when recording shortcuts.
|
|
192
|
+
*
|
|
193
|
+
* @param event - The KeyboardEvent to check
|
|
194
|
+
* @returns True if the event represents a modifier-only key
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* document.addEventListener('keydown', (event) => {
|
|
199
|
+
* if (isModifierKey(event)) {
|
|
200
|
+
* console.log('Modifier key pressed, waiting for action key...')
|
|
201
|
+
* return
|
|
202
|
+
* }
|
|
203
|
+
* // Process non-modifier key
|
|
204
|
+
* })
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
function isModifierKey(event) {
|
|
208
|
+
const key = event.key;
|
|
209
|
+
return key === "Control" || key === "Shift" || key === "Alt" || key === "Meta" || key === "Command" || key === "OS" || key === "Win";
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
213
|
+
*
|
|
214
|
+
* This is useful for validating that a recorded hotkey is complete and not
|
|
215
|
+
* just a combination of modifiers without an action key.
|
|
216
|
+
*
|
|
217
|
+
* @param hotkey - The hotkey string or ParsedHotkey to check
|
|
218
|
+
* @param platform - The target platform for parsing (defaults to auto-detection)
|
|
219
|
+
* @returns True if the hotkey contains at least one non-modifier key
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* hasNonModifierKey('Control+Shift+S') // true
|
|
224
|
+
* hasNonModifierKey('Control+Shift') // false (no action key)
|
|
225
|
+
* hasNonModifierKey(parseHotkey('Mod+A')) // true
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
function hasNonModifierKey(hotkey, platform = require_constants.detectPlatform()) {
|
|
229
|
+
const parsed = typeof hotkey === "string" ? parseHotkey(hotkey, platform) : hotkey;
|
|
230
|
+
return !isModifier(parsed.key) && parsed.key.length > 0;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Converts a hotkey string to use 'Mod' format for portability.
|
|
234
|
+
*
|
|
235
|
+
* On macOS, converts 'Meta' to 'Mod'. On Windows/Linux, converts 'Control' to 'Mod'.
|
|
236
|
+
* This enables cross-platform hotkey definitions that work consistently.
|
|
237
|
+
*
|
|
238
|
+
* @param hotkey - The hotkey string to convert
|
|
239
|
+
* @param platform - The target platform (defaults to auto-detection)
|
|
240
|
+
* @returns The hotkey string with 'Mod' format applied
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* convertToModFormat('Meta+S', 'mac') // 'Mod+S'
|
|
245
|
+
* convertToModFormat('Control+S', 'windows') // 'Mod+S'
|
|
246
|
+
* convertToModFormat('Control+Meta+S', 'mac') // 'Control+Meta+S' (both present, no conversion)
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
function convertToModFormat(hotkey, platform = require_constants.detectPlatform()) {
|
|
250
|
+
const parsed = parseHotkey(hotkey, platform);
|
|
251
|
+
if (platform === "mac" && parsed.meta && !parsed.ctrl) return hotkey.split("+").map((part) => part === "Meta" ? "Mod" : part).join("+");
|
|
252
|
+
else if (platform !== "mac" && parsed.ctrl && !parsed.meta) return hotkey.split("+").map((part) => part === "Control" ? "Mod" : part).join("+");
|
|
253
|
+
return hotkey;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
257
|
+
exports.convertToModFormat = convertToModFormat;
|
|
258
|
+
exports.hasNonModifierKey = hasNonModifierKey;
|
|
259
|
+
exports.isModifier = isModifier;
|
|
260
|
+
exports.isModifierKey = isModifierKey;
|
|
261
|
+
exports.keyboardEventToHotkey = keyboardEventToHotkey;
|
|
262
|
+
exports.normalizeHotkey = normalizeHotkey;
|
|
263
|
+
exports.parseHotkey = parseHotkey;
|
|
264
|
+
exports.parseKeyboardEvent = parseKeyboardEvent;
|
|
265
|
+
exports.rawHotkeyToParsedHotkey = rawHotkeyToParsedHotkey;
|
|
266
|
+
//# sourceMappingURL=parse.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.cjs","names":["detectPlatform","normalizeKeyName","MODIFIER_ALIASES","resolveModifier","MODIFIER_ORDER"],"sources":["../src/parse.ts"],"sourcesContent":["import {\n MODIFIER_ALIASES,\n MODIFIER_ORDER,\n detectPlatform,\n normalizeKeyName,\n resolveModifier,\n} from './constants'\nimport type {\n CanonicalModifier,\n Hotkey,\n Key,\n ParsedHotkey,\n RawHotkey,\n} from './hotkey'\n\n/**\n * Parses a hotkey string into its component parts.\n *\n * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey object with the key and modifier flags\n *\n * @example\n * ```ts\n * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }\n * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function parseHotkey(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n const parts = hotkey.split('+')\n const modifiers: Set<CanonicalModifier> = new Set()\n let key = ''\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!.trim()\n\n if (i === parts.length - 1) {\n // Last part is always the key\n key = normalizeKeyName(part)\n } else {\n // All other parts are modifiers\n const alias =\n MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()]\n\n if (alias) {\n const resolved = resolveModifier(alias, platform)\n modifiers.add(resolved)\n } else {\n // Unknown modifier, treat as part of the key if it's the only part\n // or ignore if there are more parts\n if (parts.length === 1) {\n key = normalizeKeyName(part)\n }\n }\n }\n }\n\n // If no key was found (empty string), use the last part as-is\n if (!key && parts.length > 0) {\n key = normalizeKeyName(parts[parts.length - 1]!.trim())\n }\n\n return {\n key,\n ctrl: modifiers.has('Control'),\n shift: modifiers.has('Shift'),\n alt: modifiers.has('Alt'),\n meta: modifiers.has('Meta'),\n modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m)),\n }\n}\n\n/**\n * Converts a RawHotkey object to a ParsedHotkey.\n * Optional modifier booleans default to false; modifiers array is derived from them.\n * When `mod` is true, it is resolved to Control or Meta based on platform.\n *\n * @param raw - The raw hotkey object\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey suitable for matching and formatting\n *\n * @example\n * ```ts\n * rawHotkeyToParsedHotkey({ key: 'Escape' })\n * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')\n * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')\n * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function rawHotkeyToParsedHotkey(\n raw: RawHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n let ctrl = raw.ctrl ?? false\n const shift = raw.shift ?? false\n const alt = raw.alt ?? false\n let meta = raw.meta ?? false\n\n if (raw.mod) {\n const resolved = resolveModifier('Mod', platform)\n if (resolved === 'Control') {\n ctrl = true\n } else {\n meta = true\n }\n }\n\n const modifiers: Array<CanonicalModifier> = MODIFIER_ORDER.filter((m) => {\n switch (m) {\n case 'Control':\n return ctrl\n case 'Shift':\n return shift\n case 'Alt':\n return alt\n case 'Meta':\n return meta\n default:\n return false\n }\n })\n return {\n key: raw.key,\n ctrl,\n shift,\n alt,\n meta,\n modifiers,\n }\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * The canonical form uses:\n * - Full modifier names (Control, Alt, Shift, Meta)\n * - Modifiers in order: Control+Alt+Shift+Meta\n * - Uppercase letters for single-character keys\n * - Proper casing for special keys (Escape, not escape)\n *\n * @param hotkey - The hotkey string to normalize\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns The normalized hotkey string\n *\n * @example\n * ```ts\n * normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'\n * normalizeHotkey('ctrl+a') // 'Control+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): string {\n const parsed = parseHotkey(hotkey, platform)\n const parts: Array<string> = []\n\n // Add modifiers in canonical order\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n\n // Add the key\n parts.push(parsed.key)\n\n return parts.join('+')\n}\n\n/**\n * Checks if a string represents a modifier key.\n *\n * @param key - The string to check\n * @returns True if the string is a recognized modifier\n */\nexport function isModifier(key: string): boolean {\n return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES\n}\n\n/**\n * Parses a KeyboardEvent into a ParsedHotkey object.\n *\n * This function extracts the key and modifier state from a keyboard event\n * and converts it into the same format used by `parseHotkey()`.\n *\n * @param event - The KeyboardEvent to parse\n * @param platform - The target platform for resolving modifiers (defaults to auto-detection)\n * @returns A ParsedHotkey object representing the keyboard event\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const parsed = parseKeyboardEvent(event)\n * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }\n * })\n * ```\n */\nexport function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey {\n const normalizedKey = normalizeKeyName(event.key)\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (event.ctrlKey) modifiers.push('Control')\n if (event.altKey) modifiers.push('Alt')\n if (event.shiftKey) modifiers.push('Shift')\n if (event.metaKey) modifiers.push('Meta')\n\n return {\n key: normalizedKey,\n ctrl: event.ctrlKey,\n shift: event.shiftKey,\n alt: event.altKey,\n meta: event.metaKey,\n modifiers,\n }\n}\n\n/**\n * Converts a KeyboardEvent directly to a hotkey string.\n *\n * This is a convenience function that combines `parseKeyboardEvent()` and formatting.\n * The resulting hotkey string uses canonical modifier names (Control, Alt, Shift, Meta)\n * and is suitable for use with `useHotkey()` and other hotkey functions.\n *\n * @param event - The KeyboardEvent to convert\n * @param platform - The target platform (defaults to auto-detection)\n * @returns A hotkey string in canonical form (e.g., 'Control+Shift+S')\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const hotkey = keyboardEventToHotkey(event)\n * console.log(hotkey) // 'Control+Shift+S'\n * useHotkey(hotkey, () => console.log('Shortcut triggered'))\n * })\n * ```\n */\nexport function keyboardEventToHotkey(event: KeyboardEvent): Hotkey {\n const parsed = parseKeyboardEvent(event)\n\n // Build hotkey string in canonical order (same as formatHotkey)\n const parts: Array<string> = []\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n parts.push(parsed.key)\n\n return parts.join('+') as Hotkey\n}\n\n/**\n * Checks if a KeyboardEvent represents a modifier-only key press.\n *\n * Modifier-only keys are keys like 'Control', 'Shift', 'Alt', 'Meta', etc.\n * that don't have an associated character or action key. This is useful\n * for filtering out modifier key presses when recording shortcuts.\n *\n * @param event - The KeyboardEvent to check\n * @returns True if the event represents a modifier-only key\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * if (isModifierKey(event)) {\n * console.log('Modifier key pressed, waiting for action key...')\n * return\n * }\n * // Process non-modifier key\n * })\n * ```\n */\nexport function isModifierKey(event: KeyboardEvent): boolean {\n const key = event.key\n return (\n key === 'Control' ||\n key === 'Shift' ||\n key === 'Alt' ||\n key === 'Meta' ||\n key === 'Command' ||\n key === 'OS' ||\n key === 'Win'\n )\n}\n\n/**\n * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.\n *\n * This is useful for validating that a recorded hotkey is complete and not\n * just a combination of modifiers without an action key.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to check\n * @param platform - The target platform for parsing (defaults to auto-detection)\n * @returns True if the hotkey contains at least one non-modifier key\n *\n * @example\n * ```ts\n * hasNonModifierKey('Control+Shift+S') // true\n * hasNonModifierKey('Control+Shift') // false (no action key)\n * hasNonModifierKey(parseHotkey('Mod+A')) // true\n * ```\n */\nexport function hasNonModifierKey(\n hotkey: Hotkey | ParsedHotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check if the key part is actually a modifier\n const keyIsModifier = isModifier(parsed.key)\n\n // A valid hotkey must have a non-modifier key\n return !keyIsModifier && parsed.key.length > 0\n}\n\n/**\n * Converts a hotkey string to use 'Mod' format for portability.\n *\n * On macOS, converts 'Meta' to 'Mod'. On Windows/Linux, converts 'Control' to 'Mod'.\n * This enables cross-platform hotkey definitions that work consistently.\n *\n * @param hotkey - The hotkey string to convert\n * @param platform - The target platform (defaults to auto-detection)\n * @returns The hotkey string with 'Mod' format applied\n *\n * @example\n * ```ts\n * convertToModFormat('Meta+S', 'mac') // 'Mod+S'\n * convertToModFormat('Control+S', 'windows') // 'Mod+S'\n * convertToModFormat('Control+Meta+S', 'mac') // 'Control+Meta+S' (both present, no conversion)\n * ```\n */\nexport function convertToModFormat(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n const parsed = parseHotkey(hotkey, platform)\n\n // Only convert if we have exactly one primary modifier\n if (platform === 'mac' && parsed.meta && !parsed.ctrl) {\n // Convert Meta to Mod on Mac\n const parts = hotkey.split('+')\n return parts\n .map((part) => (part === 'Meta' ? 'Mod' : part))\n .join('+') as Hotkey\n } else if (platform !== 'mac' && parsed.ctrl && !parsed.meta) {\n // Convert Control to Mod on Windows/Linux\n const parts = hotkey.split('+')\n return parts\n .map((part) => (part === 'Control' ? 'Mod' : part))\n .join('+') as Hotkey\n }\n\n // No conversion needed\n return hotkey as Hotkey\n}\n"],"mappings":"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"}
|
package/dist/parse.d.cts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Hotkey, Key, ParsedHotkey, RawHotkey } from "./hotkey.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/parse.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Parses a hotkey string into its component parts.
|
|
6
|
+
*
|
|
7
|
+
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
8
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
9
|
+
* @returns A ParsedHotkey object with the key and modifier flags
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
14
|
+
* parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }
|
|
15
|
+
* parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
declare function parseHotkey(hotkey: Hotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
|
|
19
|
+
/**
|
|
20
|
+
* Converts a RawHotkey object to a ParsedHotkey.
|
|
21
|
+
* Optional modifier booleans default to false; modifiers array is derived from them.
|
|
22
|
+
* When `mod` is true, it is resolved to Control or Meta based on platform.
|
|
23
|
+
*
|
|
24
|
+
* @param raw - The raw hotkey object
|
|
25
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
26
|
+
* @returns A ParsedHotkey suitable for matching and formatting
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* rawHotkeyToParsedHotkey({ key: 'Escape' })
|
|
31
|
+
* // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }
|
|
32
|
+
*
|
|
33
|
+
* rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')
|
|
34
|
+
* // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
35
|
+
*
|
|
36
|
+
* rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')
|
|
37
|
+
* // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function rawHotkeyToParsedHotkey(raw: RawHotkey, platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
|
|
41
|
+
/**
|
|
42
|
+
* Normalizes a hotkey string to its canonical form.
|
|
43
|
+
*
|
|
44
|
+
* The canonical form uses:
|
|
45
|
+
* - Full modifier names (Control, Alt, Shift, Meta)
|
|
46
|
+
* - Modifiers in order: Control+Alt+Shift+Meta
|
|
47
|
+
* - Uppercase letters for single-character keys
|
|
48
|
+
* - Proper casing for special keys (Escape, not escape)
|
|
49
|
+
*
|
|
50
|
+
* @param hotkey - The hotkey string to normalize
|
|
51
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
52
|
+
* @returns The normalized hotkey string
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'
|
|
57
|
+
* normalizeHotkey('ctrl+a') // 'Control+A'
|
|
58
|
+
* normalizeHotkey('esc') // 'Escape'
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'): string;
|
|
62
|
+
/**
|
|
63
|
+
* Checks if a string represents a modifier key.
|
|
64
|
+
*
|
|
65
|
+
* @param key - The string to check
|
|
66
|
+
* @returns True if the string is a recognized modifier
|
|
67
|
+
*/
|
|
68
|
+
declare function isModifier(key: string): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Parses a KeyboardEvent into a ParsedHotkey object.
|
|
71
|
+
*
|
|
72
|
+
* This function extracts the key and modifier state from a keyboard event
|
|
73
|
+
* and converts it into the same format used by `parseHotkey()`.
|
|
74
|
+
*
|
|
75
|
+
* @param event - The KeyboardEvent to parse
|
|
76
|
+
* @param platform - The target platform for resolving modifiers (defaults to auto-detection)
|
|
77
|
+
* @returns A ParsedHotkey object representing the keyboard event
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* document.addEventListener('keydown', (event) => {
|
|
82
|
+
* const parsed = parseKeyboardEvent(event)
|
|
83
|
+
* console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }
|
|
84
|
+
* })
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
|
|
88
|
+
/**
|
|
89
|
+
* Converts a KeyboardEvent directly to a hotkey string.
|
|
90
|
+
*
|
|
91
|
+
* This is a convenience function that combines `parseKeyboardEvent()` and formatting.
|
|
92
|
+
* The resulting hotkey string uses canonical modifier names (Control, Alt, Shift, Meta)
|
|
93
|
+
* and is suitable for use with `useHotkey()` and other hotkey functions.
|
|
94
|
+
*
|
|
95
|
+
* @param event - The KeyboardEvent to convert
|
|
96
|
+
* @param platform - The target platform (defaults to auto-detection)
|
|
97
|
+
* @returns A hotkey string in canonical form (e.g., 'Control+Shift+S')
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* document.addEventListener('keydown', (event) => {
|
|
102
|
+
* const hotkey = keyboardEventToHotkey(event)
|
|
103
|
+
* console.log(hotkey) // 'Control+Shift+S'
|
|
104
|
+
* useHotkey(hotkey, () => console.log('Shortcut triggered'))
|
|
105
|
+
* })
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare function keyboardEventToHotkey(event: KeyboardEvent): Hotkey;
|
|
109
|
+
/**
|
|
110
|
+
* Checks if a KeyboardEvent represents a modifier-only key press.
|
|
111
|
+
*
|
|
112
|
+
* Modifier-only keys are keys like 'Control', 'Shift', 'Alt', 'Meta', etc.
|
|
113
|
+
* that don't have an associated character or action key. This is useful
|
|
114
|
+
* for filtering out modifier key presses when recording shortcuts.
|
|
115
|
+
*
|
|
116
|
+
* @param event - The KeyboardEvent to check
|
|
117
|
+
* @returns True if the event represents a modifier-only key
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* document.addEventListener('keydown', (event) => {
|
|
122
|
+
* if (isModifierKey(event)) {
|
|
123
|
+
* console.log('Modifier key pressed, waiting for action key...')
|
|
124
|
+
* return
|
|
125
|
+
* }
|
|
126
|
+
* // Process non-modifier key
|
|
127
|
+
* })
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
declare function isModifierKey(event: KeyboardEvent): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
133
|
+
*
|
|
134
|
+
* This is useful for validating that a recorded hotkey is complete and not
|
|
135
|
+
* just a combination of modifiers without an action key.
|
|
136
|
+
*
|
|
137
|
+
* @param hotkey - The hotkey string or ParsedHotkey to check
|
|
138
|
+
* @param platform - The target platform for parsing (defaults to auto-detection)
|
|
139
|
+
* @returns True if the hotkey contains at least one non-modifier key
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* hasNonModifierKey('Control+Shift+S') // true
|
|
144
|
+
* hasNonModifierKey('Control+Shift') // false (no action key)
|
|
145
|
+
* hasNonModifierKey(parseHotkey('Mod+A')) // true
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
declare function hasNonModifierKey(hotkey: Hotkey | ParsedHotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): boolean;
|
|
149
|
+
/**
|
|
150
|
+
* Converts a hotkey string to use 'Mod' format for portability.
|
|
151
|
+
*
|
|
152
|
+
* On macOS, converts 'Meta' to 'Mod'. On Windows/Linux, converts 'Control' to 'Mod'.
|
|
153
|
+
* This enables cross-platform hotkey definitions that work consistently.
|
|
154
|
+
*
|
|
155
|
+
* @param hotkey - The hotkey string to convert
|
|
156
|
+
* @param platform - The target platform (defaults to auto-detection)
|
|
157
|
+
* @returns The hotkey string with 'Mod' format applied
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* convertToModFormat('Meta+S', 'mac') // 'Mod+S'
|
|
162
|
+
* convertToModFormat('Control+S', 'windows') // 'Mod+S'
|
|
163
|
+
* convertToModFormat('Control+Meta+S', 'mac') // 'Control+Meta+S' (both present, no conversion)
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
declare function convertToModFormat(hotkey: Hotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): Hotkey;
|
|
167
|
+
//#endregion
|
|
168
|
+
export { convertToModFormat, hasNonModifierKey, isModifier, isModifierKey, keyboardEventToHotkey, normalizeHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
|
|
169
|
+
//# sourceMappingURL=parse.d.cts.map
|
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Hotkey, Key, ParsedHotkey, RawHotkey } from "./hotkey.js";
|
|
2
|
+
|
|
3
|
+
//#region src/parse.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Parses a hotkey string into its component parts.
|
|
6
|
+
*
|
|
7
|
+
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
8
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
9
|
+
* @returns A ParsedHotkey object with the key and modifier flags
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
14
|
+
* parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }
|
|
15
|
+
* parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
declare function parseHotkey(hotkey: Hotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
|
|
19
|
+
/**
|
|
20
|
+
* Converts a RawHotkey object to a ParsedHotkey.
|
|
21
|
+
* Optional modifier booleans default to false; modifiers array is derived from them.
|
|
22
|
+
* When `mod` is true, it is resolved to Control or Meta based on platform.
|
|
23
|
+
*
|
|
24
|
+
* @param raw - The raw hotkey object
|
|
25
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
26
|
+
* @returns A ParsedHotkey suitable for matching and formatting
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* rawHotkeyToParsedHotkey({ key: 'Escape' })
|
|
31
|
+
* // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }
|
|
32
|
+
*
|
|
33
|
+
* rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')
|
|
34
|
+
* // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
35
|
+
*
|
|
36
|
+
* rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')
|
|
37
|
+
* // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function rawHotkeyToParsedHotkey(raw: RawHotkey, platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
|
|
41
|
+
/**
|
|
42
|
+
* Normalizes a hotkey string to its canonical form.
|
|
43
|
+
*
|
|
44
|
+
* The canonical form uses:
|
|
45
|
+
* - Full modifier names (Control, Alt, Shift, Meta)
|
|
46
|
+
* - Modifiers in order: Control+Alt+Shift+Meta
|
|
47
|
+
* - Uppercase letters for single-character keys
|
|
48
|
+
* - Proper casing for special keys (Escape, not escape)
|
|
49
|
+
*
|
|
50
|
+
* @param hotkey - The hotkey string to normalize
|
|
51
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
52
|
+
* @returns The normalized hotkey string
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'
|
|
57
|
+
* normalizeHotkey('ctrl+a') // 'Control+A'
|
|
58
|
+
* normalizeHotkey('esc') // 'Escape'
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'): string;
|
|
62
|
+
/**
|
|
63
|
+
* Checks if a string represents a modifier key.
|
|
64
|
+
*
|
|
65
|
+
* @param key - The string to check
|
|
66
|
+
* @returns True if the string is a recognized modifier
|
|
67
|
+
*/
|
|
68
|
+
declare function isModifier(key: string): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Parses a KeyboardEvent into a ParsedHotkey object.
|
|
71
|
+
*
|
|
72
|
+
* This function extracts the key and modifier state from a keyboard event
|
|
73
|
+
* and converts it into the same format used by `parseHotkey()`.
|
|
74
|
+
*
|
|
75
|
+
* @param event - The KeyboardEvent to parse
|
|
76
|
+
* @param platform - The target platform for resolving modifiers (defaults to auto-detection)
|
|
77
|
+
* @returns A ParsedHotkey object representing the keyboard event
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* document.addEventListener('keydown', (event) => {
|
|
82
|
+
* const parsed = parseKeyboardEvent(event)
|
|
83
|
+
* console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }
|
|
84
|
+
* })
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
|
|
88
|
+
/**
|
|
89
|
+
* Converts a KeyboardEvent directly to a hotkey string.
|
|
90
|
+
*
|
|
91
|
+
* This is a convenience function that combines `parseKeyboardEvent()` and formatting.
|
|
92
|
+
* The resulting hotkey string uses canonical modifier names (Control, Alt, Shift, Meta)
|
|
93
|
+
* and is suitable for use with `useHotkey()` and other hotkey functions.
|
|
94
|
+
*
|
|
95
|
+
* @param event - The KeyboardEvent to convert
|
|
96
|
+
* @param platform - The target platform (defaults to auto-detection)
|
|
97
|
+
* @returns A hotkey string in canonical form (e.g., 'Control+Shift+S')
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* document.addEventListener('keydown', (event) => {
|
|
102
|
+
* const hotkey = keyboardEventToHotkey(event)
|
|
103
|
+
* console.log(hotkey) // 'Control+Shift+S'
|
|
104
|
+
* useHotkey(hotkey, () => console.log('Shortcut triggered'))
|
|
105
|
+
* })
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare function keyboardEventToHotkey(event: KeyboardEvent): Hotkey;
|
|
109
|
+
/**
|
|
110
|
+
* Checks if a KeyboardEvent represents a modifier-only key press.
|
|
111
|
+
*
|
|
112
|
+
* Modifier-only keys are keys like 'Control', 'Shift', 'Alt', 'Meta', etc.
|
|
113
|
+
* that don't have an associated character or action key. This is useful
|
|
114
|
+
* for filtering out modifier key presses when recording shortcuts.
|
|
115
|
+
*
|
|
116
|
+
* @param event - The KeyboardEvent to check
|
|
117
|
+
* @returns True if the event represents a modifier-only key
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* document.addEventListener('keydown', (event) => {
|
|
122
|
+
* if (isModifierKey(event)) {
|
|
123
|
+
* console.log('Modifier key pressed, waiting for action key...')
|
|
124
|
+
* return
|
|
125
|
+
* }
|
|
126
|
+
* // Process non-modifier key
|
|
127
|
+
* })
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
declare function isModifierKey(event: KeyboardEvent): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
133
|
+
*
|
|
134
|
+
* This is useful for validating that a recorded hotkey is complete and not
|
|
135
|
+
* just a combination of modifiers without an action key.
|
|
136
|
+
*
|
|
137
|
+
* @param hotkey - The hotkey string or ParsedHotkey to check
|
|
138
|
+
* @param platform - The target platform for parsing (defaults to auto-detection)
|
|
139
|
+
* @returns True if the hotkey contains at least one non-modifier key
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* hasNonModifierKey('Control+Shift+S') // true
|
|
144
|
+
* hasNonModifierKey('Control+Shift') // false (no action key)
|
|
145
|
+
* hasNonModifierKey(parseHotkey('Mod+A')) // true
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
declare function hasNonModifierKey(hotkey: Hotkey | ParsedHotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): boolean;
|
|
149
|
+
/**
|
|
150
|
+
* Converts a hotkey string to use 'Mod' format for portability.
|
|
151
|
+
*
|
|
152
|
+
* On macOS, converts 'Meta' to 'Mod'. On Windows/Linux, converts 'Control' to 'Mod'.
|
|
153
|
+
* This enables cross-platform hotkey definitions that work consistently.
|
|
154
|
+
*
|
|
155
|
+
* @param hotkey - The hotkey string to convert
|
|
156
|
+
* @param platform - The target platform (defaults to auto-detection)
|
|
157
|
+
* @returns The hotkey string with 'Mod' format applied
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* convertToModFormat('Meta+S', 'mac') // 'Mod+S'
|
|
162
|
+
* convertToModFormat('Control+S', 'windows') // 'Mod+S'
|
|
163
|
+
* convertToModFormat('Control+Meta+S', 'mac') // 'Control+Meta+S' (both present, no conversion)
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
declare function convertToModFormat(hotkey: Hotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): Hotkey;
|
|
167
|
+
//#endregion
|
|
168
|
+
export { convertToModFormat, hasNonModifierKey, isModifier, isModifierKey, keyboardEventToHotkey, normalizeHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
|
|
169
|
+
//# sourceMappingURL=parse.d.ts.map
|