@tanstack/hotkeys 0.15.1 → 0.16.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/README.md +1 -1
- package/dist/_keyboard-event.js +2 -2
- package/dist/_match.js +2 -2
- package/dist/_recorder-chord.js +3 -3
- package/dist/_registration.js +8 -5
- package/dist/adapter.d.ts +53 -6
- package/dist/adapter.js +45 -3
- package/dist/app-hotkeys.d.ts +11 -8
- package/dist/app-hotkeys.js +13 -7
- package/dist/conflicts.js +5 -5
- package/dist/format.d.ts +13 -9
- package/dist/format.js +7 -16
- package/dist/hint.js +2 -2
- package/dist/hotkey-manager.d.ts +94 -11
- package/dist/hotkey-manager.js +208 -31
- package/dist/hotkey-recorder.d.ts +20 -10
- package/dist/hotkey-recorder.js +9 -5
- package/dist/hotkey.types.d.ts +59 -32
- package/dist/index.d.ts +6 -6
- package/dist/index.js +3 -3
- package/dist/match.d.ts +19 -18
- package/dist/match.js +24 -21
- package/dist/parse.d.ts +35 -73
- package/dist/parse.js +39 -108
- package/dist/validate.js +2 -2
- package/package.json +1 -1
package/dist/parse.d.ts
CHANGED
|
@@ -1,61 +1,42 @@
|
|
|
1
1
|
import { Key } from "./key.types.js";
|
|
2
|
-
import { Hotkey, HotkeyChord, HotkeySequence,
|
|
2
|
+
import { Hotkey, HotkeyChord, HotkeySequence, ParsedHotkeyChord, RegisterableHotkey, RegisterableHotkeyChord, RegisterableHotkeySequence } from "./hotkey.types.js";
|
|
3
3
|
//#region src/parse.d.ts
|
|
4
|
+
type Platform = 'mac' | 'windows' | 'linux';
|
|
5
|
+
/** A chord in any form: a string, a chord object, or an already-parsed chord. */
|
|
6
|
+
type AnyChord = RegisterableHotkeyChord | ParsedHotkeyChord | (string & {});
|
|
4
7
|
/**
|
|
5
8
|
* Whether a hotkey is a sequence. Use this instead of `Array.isArray`, which
|
|
6
9
|
* does not narrow readonly arrays.
|
|
7
10
|
*/
|
|
8
|
-
export declare function isHotkeySequence(hotkey:
|
|
11
|
+
export declare function isHotkeySequence<T extends RegisterableHotkey>(hotkey: T): hotkey is Extract<T, ReadonlyArray<unknown>>;
|
|
9
12
|
/**
|
|
10
13
|
* Collapses a one-step sequence to its chord without otherwise changing the hotkey.
|
|
11
14
|
* Registration APIs treat `['Mod+S']` exactly like `'Mod+S'`.
|
|
12
15
|
*/
|
|
13
|
-
export declare function simplifyHotkey<T extends RegisterableHotkey>(hotkey: T): T extends HotkeySequence ? Hotkey : T;
|
|
16
|
+
export declare function simplifyHotkey<T extends RegisterableHotkey>(hotkey: T): T extends HotkeySequence ? Hotkey : T extends RegisterableHotkeySequence ? RegisterableHotkey : T;
|
|
14
17
|
/**
|
|
15
|
-
* Parses a hotkey
|
|
18
|
+
* Parses a hotkey, resolving `Mod` for the platform. A chord, written as a
|
|
19
|
+
* string or an object, returns one parsed chord. A sequence returns one
|
|
20
|
+
* parsed chord per step. Physical tokens such as `[KeyQ]` keep `code`; they
|
|
21
|
+
* never become a logical Q.
|
|
16
22
|
*
|
|
17
|
-
* @param hotkey -
|
|
23
|
+
* @param hotkey - A chord string such as `'Mod+S'`, a chord object, or a sequence
|
|
18
24
|
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
19
|
-
* @returns A ParsedHotkey with either logical key or physical code and modifier flags
|
|
20
25
|
*
|
|
21
26
|
* @example
|
|
22
27
|
* ```ts
|
|
23
|
-
* parseHotkey('Mod+S') //
|
|
24
|
-
* parseHotkey(
|
|
25
|
-
* parseHotkey('
|
|
28
|
+
* parseHotkey('Mod+S', 'mac') // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
29
|
+
* parseHotkey({ key: 'S', mod: true }, 'windows') // { key: 'S', ctrl: true, ..., modifiers: ['Control'] }
|
|
30
|
+
* parseHotkey(['G', 'Shift+G']) // [{ key: 'G', ... }, { key: 'G', shift: true, ... }]
|
|
26
31
|
* ```
|
|
27
32
|
*/
|
|
28
|
-
export declare function parseHotkey(hotkey:
|
|
33
|
+
export declare function parseHotkey(hotkey: AnyChord, platform?: Platform): ParsedHotkeyChord;
|
|
34
|
+
export declare function parseHotkey(hotkey: ReadonlyArray<AnyChord>, platform?: Platform): Array<ParsedHotkeyChord>;
|
|
35
|
+
export declare function parseHotkey(hotkey: AnyChord | ReadonlyArray<AnyChord>, platform?: Platform): ParsedHotkeyChord | Array<ParsedHotkeyChord>;
|
|
29
36
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @param raw - The raw hotkey object
|
|
35
|
-
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
36
|
-
* @returns A ParsedHotkey suitable for matching and formatting
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* rawHotkeyToParsedHotkey({ key: 'Escape' })
|
|
41
|
-
* // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }
|
|
42
|
-
*
|
|
43
|
-
* rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')
|
|
44
|
-
* // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
45
|
-
*
|
|
46
|
-
* rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')
|
|
47
|
-
* // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export declare function rawHotkeyToParsedHotkey(raw: RawHotkey, platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
|
|
51
|
-
/**
|
|
52
|
-
* Same canonical string as {@link normalizeHotkey}, but from an already-parsed
|
|
53
|
-
* hotkey: `Mod` first when the platform allows, then `Alt`, then `Shift`;
|
|
54
|
-
* otherwise full `Control+Alt+Shift+Meta` order.
|
|
55
|
-
*/
|
|
56
|
-
export declare function normalizeHotkeyFromParsed(parsed: ParsedHotkey, platform?: 'mac' | 'windows' | 'linux'): HotkeyChord;
|
|
57
|
-
/**
|
|
58
|
-
* Normalizes a hotkey string to its canonical form.
|
|
37
|
+
* Normalizes a hotkey to its canonical string form. Accepts a chord string, a
|
|
38
|
+
* chord object, a parsed chord, or a sequence of any of these. Each sequence
|
|
39
|
+
* step is normalized, and a one-step sequence becomes its chord.
|
|
59
40
|
*
|
|
60
41
|
* - When `Mod` is allowed for the platform (Command on Mac without Control;
|
|
61
42
|
* Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
|
|
@@ -63,25 +44,20 @@ export declare function normalizeHotkeyFromParsed(parsed: ParsedHotkey, platform
|
|
|
63
44
|
* - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
|
|
64
45
|
* - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
|
|
65
46
|
*
|
|
66
|
-
* @param hotkey - The hotkey
|
|
47
|
+
* @param hotkey - The hotkey to normalize
|
|
67
48
|
* @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
|
|
68
|
-
* @returns The normalized hotkey string
|
|
69
49
|
*
|
|
70
50
|
* @example
|
|
71
51
|
* ```ts
|
|
72
52
|
* normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
|
|
73
53
|
* normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
|
|
74
|
-
* normalizeHotkey('
|
|
54
|
+
* normalizeHotkey({ key: 's', mod: true }) // 'Mod+S'
|
|
55
|
+
* normalizeHotkey(['g', 'shift+g']) // ['G', 'Shift+G']
|
|
56
|
+
* normalizeHotkey(parseKeyboardEvent(event)) // The chord the event pressed
|
|
75
57
|
* ```
|
|
76
58
|
*/
|
|
77
|
-
export declare function normalizeHotkey(hotkey:
|
|
78
|
-
|
|
79
|
-
* Normalizes a chord (string or {@link RawHotkey} object) or a sequence to canonical form.
|
|
80
|
-
* Each sequence step is normalized, and a one-step sequence becomes its chord.
|
|
81
|
-
* Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
|
|
82
|
-
*/
|
|
83
|
-
export declare function normalizeRegisterableHotkey(hotkey: RegisterableHotkeyChord, platform?: 'mac' | 'windows' | 'linux'): HotkeyChord;
|
|
84
|
-
export declare function normalizeRegisterableHotkey(hotkey: RegisterableHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
|
|
59
|
+
export declare function normalizeHotkey(hotkey: AnyChord, platform?: Platform): HotkeyChord;
|
|
60
|
+
export declare function normalizeHotkey(hotkey: AnyChord | ReadonlyArray<AnyChord>, platform?: Platform): Hotkey;
|
|
85
61
|
/**
|
|
86
62
|
* Checks if a string is a recognized modifier token (including aliases).
|
|
87
63
|
*
|
|
@@ -93,14 +69,12 @@ export declare function normalizeRegisterableHotkey(hotkey: RegisterableHotkey,
|
|
|
93
69
|
*/
|
|
94
70
|
export declare function isModifierKey(key: Key | (string & {})): boolean;
|
|
95
71
|
/**
|
|
96
|
-
* Parses a KeyboardEvent into
|
|
97
|
-
*
|
|
98
|
-
* This function extracts the key and modifier state from a keyboard event
|
|
99
|
-
* and converts it into the same format used by `parseHotkey()`.
|
|
72
|
+
* Parses a KeyboardEvent into the chord it pressed, in the same format as
|
|
73
|
+
* `parseHotkey()`. Pass the result to `normalizeHotkey` for its canonical string.
|
|
100
74
|
*
|
|
101
75
|
* @param event - The KeyboardEvent to parse
|
|
102
76
|
* @param platform - The target platform for resolving modifiers (defaults to auto-detection)
|
|
103
|
-
* @returns
|
|
77
|
+
* @returns The parsed chord for the keyboard event
|
|
104
78
|
*
|
|
105
79
|
* @example
|
|
106
80
|
* ```ts
|
|
@@ -110,23 +84,16 @@ export declare function isModifierKey(key: Key | (string & {})): boolean;
|
|
|
110
84
|
* })
|
|
111
85
|
* ```
|
|
112
86
|
*/
|
|
113
|
-
export declare function parseKeyboardEvent(event: KeyboardEvent, platform?:
|
|
87
|
+
export declare function parseKeyboardEvent(event: KeyboardEvent, platform?: Platform): ParsedHotkeyChord;
|
|
114
88
|
/**
|
|
115
|
-
*
|
|
89
|
+
* Checks if a chord contains at least one non-modifier key.
|
|
116
90
|
*
|
|
117
|
-
*
|
|
118
|
-
* @param platform - Target platform for `Mod` eligibility
|
|
119
|
-
*/
|
|
120
|
-
export declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): HotkeyChord;
|
|
121
|
-
/**
|
|
122
|
-
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
123
|
-
*
|
|
124
|
-
* This is useful for validating that a recorded hotkey is complete and not
|
|
91
|
+
* This is useful for validating that a recorded chord is complete and not
|
|
125
92
|
* just a combination of modifiers without an action key.
|
|
126
93
|
*
|
|
127
|
-
* @param
|
|
94
|
+
* @param chord - The chord string or parsed chord to check
|
|
128
95
|
* @param platform - The target platform for parsing (defaults to auto-detection)
|
|
129
|
-
* @returns True if the
|
|
96
|
+
* @returns True if the chord contains at least one non-modifier key
|
|
130
97
|
*
|
|
131
98
|
* @example
|
|
132
99
|
* ```ts
|
|
@@ -135,10 +102,5 @@ export declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?
|
|
|
135
102
|
* hasNonModifierKey(parseHotkey('Mod+A')) // true
|
|
136
103
|
* ```
|
|
137
104
|
*/
|
|
138
|
-
export declare function hasNonModifierKey(
|
|
139
|
-
/**
|
|
140
|
-
* Parses a string or raw binding, resolving Mod for the supplied platform.
|
|
141
|
-
* Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
|
|
142
|
-
*/
|
|
143
|
-
export declare function parseRegisterableHotkey(hotkey: RegisterableHotkeyChord, platform?: "mac" | "windows" | "linux"): ParsedHotkey;
|
|
105
|
+
export declare function hasNonModifierKey(chord: HotkeyChord | ParsedHotkeyChord | (string & {}), platform?: Platform): boolean;
|
|
144
106
|
//#endregion
|
package/dist/parse.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { MODIFIER_ORDER, normalizeKeyName } from "./constants.js";
|
|
2
2
|
import { detectPlatform, resolveModifier } from "./platform.js";
|
|
3
|
-
import { assertLogicalKey, modifierAlias, normalizeKeyboardEvent, physicalCodeOf,
|
|
3
|
+
import { assertLogicalKey, modifierAlias, normalizeKeyboardEvent, physicalCodeOf, splitChordParts } from "./_keyboard-event.js";
|
|
4
4
|
|
|
5
5
|
//#region src/parse.ts
|
|
6
|
-
/** Builds a
|
|
7
|
-
function
|
|
6
|
+
/** Builds a ParsedHotkeyChord, listing held modifiers in canonical order. */
|
|
7
|
+
function toParsedChord(identity, ctrl, shift, alt, meta) {
|
|
8
8
|
const held = {
|
|
9
9
|
Control: ctrl,
|
|
10
10
|
Alt: alt,
|
|
@@ -30,22 +30,8 @@ function isHotkeySequence(hotkey) {
|
|
|
30
30
|
function simplifyHotkey(hotkey) {
|
|
31
31
|
return isHotkeySequence(hotkey) && hotkey.length === 1 ? hotkey[0] : hotkey;
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
37
|
-
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
38
|
-
* @returns A ParsedHotkey with either logical key or physical code and modifier flags
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
43
|
-
* parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }
|
|
44
|
-
* parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
function parseHotkey(hotkey, platform = detectPlatform()) {
|
|
48
|
-
const parts = splitHotkeyParts(hotkey);
|
|
33
|
+
function parseChordString(chord, platform) {
|
|
34
|
+
const parts = splitChordParts(chord);
|
|
49
35
|
const modifiers = /* @__PURE__ */ new Set();
|
|
50
36
|
let key = "";
|
|
51
37
|
for (let i = 0; i < parts.length; i++) {
|
|
@@ -61,47 +47,30 @@ function parseHotkey(hotkey, platform = detectPlatform()) {
|
|
|
61
47
|
}
|
|
62
48
|
if (!key && parts.length > 0) key = normalizeKeyName(parts[parts.length - 1].trim());
|
|
63
49
|
const code = physicalCodeOf(key);
|
|
64
|
-
return
|
|
50
|
+
return toParsedChord(code !== void 0 ? { code } : { key }, modifiers.has("Control"), modifiers.has("Shift"), modifiers.has("Alt"), modifiers.has("Meta"));
|
|
65
51
|
}
|
|
66
|
-
/**
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```ts
|
|
77
|
-
* rawHotkeyToParsedHotkey({ key: 'Escape' })
|
|
78
|
-
* // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }
|
|
79
|
-
*
|
|
80
|
-
* rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')
|
|
81
|
-
* // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
82
|
-
*
|
|
83
|
-
* rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')
|
|
84
|
-
* // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
|
|
88
|
-
if (raw.code === void 0) assertLogicalKey(raw.key);
|
|
89
|
-
let ctrl = raw.ctrl ?? false;
|
|
90
|
-
const shift = raw.shift ?? false;
|
|
91
|
-
const alt = raw.alt ?? false;
|
|
92
|
-
let meta = raw.meta ?? false;
|
|
93
|
-
if (raw.mod) {
|
|
52
|
+
/** Resolves a chord object's modifiers. Omitted modifiers are false. */
|
|
53
|
+
function parseChordObject(chord, platform) {
|
|
54
|
+
if (chord.code === void 0) assertLogicalKey(chord.key);
|
|
55
|
+
let ctrl = chord.ctrl ?? false;
|
|
56
|
+
const shift = chord.shift ?? false;
|
|
57
|
+
const alt = chord.alt ?? false;
|
|
58
|
+
let meta = chord.meta ?? false;
|
|
59
|
+
if (chord.mod) {
|
|
94
60
|
if (resolveModifier("Mod", platform) === "Control") ctrl = true;
|
|
95
61
|
else meta = true;
|
|
96
62
|
}
|
|
97
|
-
return
|
|
63
|
+
return toParsedChord(chord.code !== void 0 ? { code: chord.code } : { key: normalizeKeyName(chord.key) }, ctrl, shift, alt, meta);
|
|
64
|
+
}
|
|
65
|
+
function parseChord(chord, platform) {
|
|
66
|
+
return typeof chord === "string" ? parseChordString(chord, platform) : parseChordObject(chord, platform);
|
|
98
67
|
}
|
|
99
68
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
69
|
+
* The canonical string for a parsed chord: `Mod` first when the platform
|
|
70
|
+
* allows, then `Alt`, then `Shift`; otherwise full `Control+Alt+Shift+Meta`
|
|
71
|
+
* order.
|
|
103
72
|
*/
|
|
104
|
-
function
|
|
73
|
+
function normalizeParsedChord(parsed, platform) {
|
|
105
74
|
if (parsed.code === void 0) assertLogicalKey(parsed.key);
|
|
106
75
|
const canUseMod = platform === "mac" ? parsed.meta && !parsed.ctrl : parsed.ctrl && !parsed.meta;
|
|
107
76
|
const parts = [];
|
|
@@ -113,35 +82,15 @@ function normalizeHotkeyFromParsed(parsed, platform = detectPlatform()) {
|
|
|
113
82
|
parts.push(parsed.code !== void 0 ? `[${parsed.code}]` : normalizeKeyName(parsed.key));
|
|
114
83
|
return parts.join("+");
|
|
115
84
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
*
|
|
119
|
-
* - When `Mod` is allowed for the platform (Command on Mac without Control;
|
|
120
|
-
* Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
|
|
121
|
-
* then the key (e.g. `Mod+Shift+E`, `Mod+S`).
|
|
122
|
-
* - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
|
|
123
|
-
* - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
|
|
124
|
-
*
|
|
125
|
-
* @param hotkey - The hotkey string to normalize
|
|
126
|
-
* @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
|
|
127
|
-
* @returns The normalized hotkey string
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
|
|
132
|
-
* normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
|
|
133
|
-
* normalizeHotkey('esc') // 'Escape'
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
function normalizeHotkey(hotkey, platform = detectPlatform()) {
|
|
137
|
-
return normalizeHotkeyFromParsed(parseHotkey(hotkey, platform), platform);
|
|
85
|
+
function parseHotkey(hotkey, platform = detectPlatform()) {
|
|
86
|
+
return Array.isArray(hotkey) ? hotkey.map((step) => parseChord(step, platform)) : parseChord(hotkey, platform);
|
|
138
87
|
}
|
|
139
|
-
function
|
|
140
|
-
if (
|
|
141
|
-
const steps = hotkey.map((step) =>
|
|
88
|
+
function normalizeHotkey(hotkey, platform = detectPlatform()) {
|
|
89
|
+
if (Array.isArray(hotkey)) {
|
|
90
|
+
const steps = hotkey.map((step) => normalizeParsedChord(parseChord(step, platform), platform));
|
|
142
91
|
return steps.length === 1 ? steps[0] : steps;
|
|
143
92
|
}
|
|
144
|
-
return
|
|
93
|
+
return normalizeParsedChord(parseChord(hotkey, platform), platform);
|
|
145
94
|
}
|
|
146
95
|
/**
|
|
147
96
|
* Checks if a string is a recognized modifier token (including aliases).
|
|
@@ -156,14 +105,12 @@ function isModifierKey(key) {
|
|
|
156
105
|
return modifierAlias(key) !== void 0;
|
|
157
106
|
}
|
|
158
107
|
/**
|
|
159
|
-
* Parses a KeyboardEvent into
|
|
160
|
-
*
|
|
161
|
-
* This function extracts the key and modifier state from a keyboard event
|
|
162
|
-
* and converts it into the same format used by `parseHotkey()`.
|
|
108
|
+
* Parses a KeyboardEvent into the chord it pressed, in the same format as
|
|
109
|
+
* `parseHotkey()`. Pass the result to `normalizeHotkey` for its canonical string.
|
|
163
110
|
*
|
|
164
111
|
* @param event - The KeyboardEvent to parse
|
|
165
112
|
* @param platform - The target platform for resolving modifiers (defaults to auto-detection)
|
|
166
|
-
* @returns
|
|
113
|
+
* @returns The parsed chord for the keyboard event
|
|
167
114
|
*
|
|
168
115
|
* @example
|
|
169
116
|
* ```ts
|
|
@@ -178,26 +125,17 @@ function parseKeyboardEvent(event, platform) {
|
|
|
178
125
|
const ctrl = normalized.altGraph ? false : normalized.ctrl;
|
|
179
126
|
const alt = normalized.altGraph ? false : normalized.alt;
|
|
180
127
|
const shift = normalized.shift;
|
|
181
|
-
return
|
|
128
|
+
return toParsedChord({ key: normalized.key }, ctrl, shift, alt, normalized.meta);
|
|
182
129
|
}
|
|
183
130
|
/**
|
|
184
|
-
*
|
|
131
|
+
* Checks if a chord contains at least one non-modifier key.
|
|
185
132
|
*
|
|
186
|
-
*
|
|
187
|
-
* @param platform - Target platform for `Mod` eligibility
|
|
188
|
-
*/
|
|
189
|
-
function normalizeHotkeyFromEvent(event, platform = detectPlatform()) {
|
|
190
|
-
return normalizeHotkeyFromParsed(parseKeyboardEvent(event, platform), platform);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
194
|
-
*
|
|
195
|
-
* This is useful for validating that a recorded hotkey is complete and not
|
|
133
|
+
* This is useful for validating that a recorded chord is complete and not
|
|
196
134
|
* just a combination of modifiers without an action key.
|
|
197
135
|
*
|
|
198
|
-
* @param
|
|
136
|
+
* @param chord - The chord string or parsed chord to check
|
|
199
137
|
* @param platform - The target platform for parsing (defaults to auto-detection)
|
|
200
|
-
* @returns True if the
|
|
138
|
+
* @returns True if the chord contains at least one non-modifier key
|
|
201
139
|
*
|
|
202
140
|
* @example
|
|
203
141
|
* ```ts
|
|
@@ -206,18 +144,11 @@ function normalizeHotkeyFromEvent(event, platform = detectPlatform()) {
|
|
|
206
144
|
* hasNonModifierKey(parseHotkey('Mod+A')) // true
|
|
207
145
|
* ```
|
|
208
146
|
*/
|
|
209
|
-
function hasNonModifierKey(
|
|
210
|
-
const parsed = typeof
|
|
147
|
+
function hasNonModifierKey(chord, platform = detectPlatform()) {
|
|
148
|
+
const parsed = typeof chord === "string" ? parseChordString(chord, platform) : chord;
|
|
211
149
|
if (parsed.code !== void 0) return parsed.code.length > 0 && !/^(Control|Shift|Alt|Meta)(Left|Right)$/.test(parsed.code);
|
|
212
150
|
return !isModifierKey(parsed.key) && parsed.key.length > 0;
|
|
213
151
|
}
|
|
214
|
-
/**
|
|
215
|
-
* Parses a string or raw binding, resolving Mod for the supplied platform.
|
|
216
|
-
* Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
|
|
217
|
-
*/
|
|
218
|
-
function parseRegisterableHotkey(hotkey, platform = detectPlatform()) {
|
|
219
|
-
return typeof hotkey === "string" ? parseHotkey(hotkey, platform) : rawHotkeyToParsedHotkey(hotkey, platform);
|
|
220
|
-
}
|
|
221
152
|
|
|
222
153
|
//#endregion
|
|
223
|
-
export { hasNonModifierKey, isHotkeySequence, isModifierKey, normalizeHotkey,
|
|
154
|
+
export { hasNonModifierKey, isHotkeySequence, isModifierKey, normalizeHotkey, parseHotkey, parseKeyboardEvent, simplifyHotkey };
|
package/dist/validate.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ALL_KEYS, normalizeKeyName } from "./constants.js";
|
|
2
|
-
import { modifierAlias, physicalCodeOf,
|
|
2
|
+
import { modifierAlias, physicalCodeOf, splitChordParts } from "./_keyboard-event.js";
|
|
3
3
|
|
|
4
4
|
//#region src/validate.ts
|
|
5
5
|
/**
|
|
@@ -52,7 +52,7 @@ function validateChord(hotkey) {
|
|
|
52
52
|
warnings: [],
|
|
53
53
|
errors: ["Hotkey cannot be empty"]
|
|
54
54
|
};
|
|
55
|
-
const parts =
|
|
55
|
+
const parts = splitChordParts(hotkey).map((p) => p.trim());
|
|
56
56
|
if (parts.length === 0 || parts.some((p) => p === "")) return {
|
|
57
57
|
valid: false,
|
|
58
58
|
warnings: [],
|