@tanstack/hotkeys 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +1 -1
  2. package/dist/constants.cjs +70 -10
  3. package/dist/constants.cjs.map +1 -1
  4. package/dist/constants.d.cts +57 -4
  5. package/dist/constants.d.ts +57 -4
  6. package/dist/constants.js +67 -10
  7. package/dist/constants.js.map +1 -1
  8. package/dist/format.cjs +20 -125
  9. package/dist/format.cjs.map +1 -1
  10. package/dist/format.d.cts +8 -72
  11. package/dist/format.d.ts +8 -72
  12. package/dist/format.js +23 -127
  13. package/dist/format.js.map +1 -1
  14. package/dist/hotkey-manager.cjs.map +1 -1
  15. package/dist/hotkey-manager.d.cts +5 -1
  16. package/dist/hotkey-manager.d.ts +5 -1
  17. package/dist/hotkey-manager.js.map +1 -1
  18. package/dist/hotkey.d.cts +2 -0
  19. package/dist/hotkey.d.ts +2 -0
  20. package/dist/index.cjs +7 -5
  21. package/dist/index.d.cts +4 -4
  22. package/dist/index.d.ts +4 -4
  23. package/dist/index.js +4 -4
  24. package/dist/parse.cjs +53 -91
  25. package/dist/parse.cjs.map +1 -1
  26. package/dist/parse.d.cts +31 -72
  27. package/dist/parse.d.ts +31 -72
  28. package/dist/parse.js +51 -89
  29. package/dist/parse.js.map +1 -1
  30. package/dist/recorder-chord.cjs +3 -2
  31. package/dist/recorder-chord.cjs.map +1 -1
  32. package/dist/recorder-chord.js +4 -3
  33. package/dist/recorder-chord.js.map +1 -1
  34. package/dist/sequence-manager.cjs +1 -1
  35. package/dist/sequence-manager.cjs.map +1 -1
  36. package/dist/sequence-manager.js +2 -2
  37. package/dist/sequence-manager.js.map +1 -1
  38. package/package.json +1 -1
  39. package/src/constants.ts +75 -21
  40. package/src/format.ts +58 -196
  41. package/src/hotkey-manager.ts +5 -1
  42. package/src/hotkey.ts +2 -0
  43. package/src/parse.ts +91 -131
  44. package/src/recorder-chord.ts +4 -5
  45. package/src/sequence-manager.ts +2 -2
package/dist/index.d.cts CHANGED
@@ -1,13 +1,13 @@
1
1
  import { CanonicalModifier, EditingKey, FormatDisplayOptions, FunctionKey, HeldKey, Hotkey, HotkeyCallback, HotkeyCallbackContext, Key, LetterKey, Modifier, NavigationKey, NumberKey, ParsedHotkey, PunctuationKey, RawHotkey, RegisterableHotkey, ValidationResult } from "./hotkey.cjs";
2
- import { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, KEY_DISPLAY_SYMBOLS, LETTER_KEYS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, STANDARD_MODIFIER_LABELS, detectPlatform, isSingleLetterKey, normalizeKeyName, resolveModifier } from "./constants.cjs";
3
- import { FormatKeyDebuggingOptions, formatForDisplay, formatHotkey, formatHotkeySequence, formatKeyForDebuggingDisplay, formatWithLabels } from "./format.cjs";
2
+ import { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, KEY_DISPLAY_SYMBOLS, LETTER_KEYS, LINUX_MODIFIER_LABELS, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, PUNCTUATION_KEY_DISPLAY_LABELS, WINDOWS_MODIFIER_LABELS, detectPlatform, isSingleLetterKey, normalizeKeyName, resolveModifier } from "./constants.cjs";
3
+ import { formatForDisplay, formatHotkey, formatHotkeySequence, formatWithLabels } from "./format.cjs";
4
4
  import { ConflictBehavior } from "./manager.utils.cjs";
5
5
  import { HotkeyManager, HotkeyOptions, HotkeyRegistration, HotkeyRegistrationHandle, getHotkeyManager } from "./hotkey-manager.cjs";
6
6
  import { KeyStateTracker, KeyStateTrackerState, getKeyStateTracker } from "./key-state-tracker.cjs";
7
7
  import { CreateHotkeyHandlerOptions, createHotkeyHandler, createMultiHotkeyHandler, matchesKeyboardEvent } from "./match.cjs";
8
- import { convertToModFormat, hasNonModifierKey, isModifier, isModifierKey, keyboardEventToHotkey, normalizeHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey } from "./parse.cjs";
8
+ import { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey } from "./parse.cjs";
9
9
  import { HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState } from "./hotkey-recorder.cjs";
10
10
  import { DEFAULT_SEQUENCE_TIMEOUT, HotkeySequence, SequenceManager, SequenceOptions, SequenceRegistrationHandle, SequenceRegistrationView, createSequenceMatcher, getSequenceManager } from "./sequence-manager.cjs";
11
11
  import { HotkeySequenceRecorder, HotkeySequenceRecorderCommitKeys, HotkeySequenceRecorderOptions, HotkeySequenceRecorderState } from "./hotkey-sequence-recorder.cjs";
12
12
  import { assertValidHotkey, checkHotkey, validateHotkey } from "./validate.cjs";
13
- export { ALL_KEYS, CanonicalModifier, ConflictBehavior, CreateHotkeyHandlerOptions, DEFAULT_SEQUENCE_TIMEOUT, EDITING_KEYS, EditingKey, FUNCTION_KEYS, FormatDisplayOptions, FormatKeyDebuggingOptions, FunctionKey, HeldKey, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyManager, HotkeyOptions, HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState, HotkeyRegistration, HotkeyRegistrationHandle, HotkeySequence, HotkeySequenceRecorder, HotkeySequenceRecorderCommitKeys, HotkeySequenceRecorderOptions, HotkeySequenceRecorderState, KEY_DISPLAY_SYMBOLS, Key, KeyStateTracker, KeyStateTrackerState, LETTER_KEYS, LetterKey, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, Modifier, NAVIGATION_KEYS, NUMBER_KEYS, NavigationKey, NumberKey, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, ParsedHotkey, PunctuationKey, RawHotkey, RegisterableHotkey, STANDARD_MODIFIER_LABELS, SequenceManager, SequenceOptions, SequenceRegistrationHandle, SequenceRegistrationView, ValidationResult, assertValidHotkey, checkHotkey, convertToModFormat, createHotkeyHandler, createMultiHotkeyHandler, createSequenceMatcher, detectPlatform, formatForDisplay, formatHotkey, formatHotkeySequence, formatKeyForDebuggingDisplay, formatWithLabels, getHotkeyManager, getKeyStateTracker, getSequenceManager, hasNonModifierKey, isModifier, isModifierKey, isSingleLetterKey, keyboardEventToHotkey, matchesKeyboardEvent, normalizeHotkey, normalizeKeyName, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey, resolveModifier, validateHotkey };
13
+ export { ALL_KEYS, CanonicalModifier, ConflictBehavior, CreateHotkeyHandlerOptions, DEFAULT_SEQUENCE_TIMEOUT, EDITING_KEYS, EditingKey, FUNCTION_KEYS, FormatDisplayOptions, FunctionKey, HeldKey, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyManager, HotkeyOptions, HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState, HotkeyRegistration, HotkeyRegistrationHandle, HotkeySequence, HotkeySequenceRecorder, HotkeySequenceRecorderCommitKeys, HotkeySequenceRecorderOptions, HotkeySequenceRecorderState, KEY_DISPLAY_SYMBOLS, Key, KeyStateTracker, KeyStateTrackerState, LETTER_KEYS, LINUX_MODIFIER_LABELS, LetterKey, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, Modifier, NAVIGATION_KEYS, NUMBER_KEYS, NavigationKey, NumberKey, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, PUNCTUATION_KEY_DISPLAY_LABELS, ParsedHotkey, PunctuationKey, RawHotkey, RegisterableHotkey, SequenceManager, SequenceOptions, SequenceRegistrationHandle, SequenceRegistrationView, ValidationResult, WINDOWS_MODIFIER_LABELS, assertValidHotkey, checkHotkey, createHotkeyHandler, createMultiHotkeyHandler, createSequenceMatcher, detectPlatform, formatForDisplay, formatHotkey, formatHotkeySequence, formatWithLabels, getHotkeyManager, getKeyStateTracker, getSequenceManager, hasNonModifierKey, isModifierKey, isSingleLetterKey, matchesKeyboardEvent, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeKeyName, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey, resolveModifier, validateHotkey };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import { CanonicalModifier, EditingKey, FormatDisplayOptions, FunctionKey, HeldKey, Hotkey, HotkeyCallback, HotkeyCallbackContext, Key, LetterKey, Modifier, NavigationKey, NumberKey, ParsedHotkey, PunctuationKey, RawHotkey, RegisterableHotkey, ValidationResult } from "./hotkey.js";
2
- import { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, KEY_DISPLAY_SYMBOLS, LETTER_KEYS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, STANDARD_MODIFIER_LABELS, detectPlatform, isSingleLetterKey, normalizeKeyName, resolveModifier } from "./constants.js";
3
- import { FormatKeyDebuggingOptions, formatForDisplay, formatHotkey, formatHotkeySequence, formatKeyForDebuggingDisplay, formatWithLabels } from "./format.js";
2
+ import { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, KEY_DISPLAY_SYMBOLS, LETTER_KEYS, LINUX_MODIFIER_LABELS, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, PUNCTUATION_KEY_DISPLAY_LABELS, WINDOWS_MODIFIER_LABELS, detectPlatform, isSingleLetterKey, normalizeKeyName, resolveModifier } from "./constants.js";
3
+ import { formatForDisplay, formatHotkey, formatHotkeySequence, formatWithLabels } from "./format.js";
4
4
  import { ConflictBehavior } from "./manager.utils.js";
5
5
  import { HotkeyManager, HotkeyOptions, HotkeyRegistration, HotkeyRegistrationHandle, getHotkeyManager } from "./hotkey-manager.js";
6
6
  import { KeyStateTracker, KeyStateTrackerState, getKeyStateTracker } from "./key-state-tracker.js";
7
7
  import { CreateHotkeyHandlerOptions, createHotkeyHandler, createMultiHotkeyHandler, matchesKeyboardEvent } from "./match.js";
8
- import { convertToModFormat, hasNonModifierKey, isModifier, isModifierKey, keyboardEventToHotkey, normalizeHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey } from "./parse.js";
8
+ import { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey } from "./parse.js";
9
9
  import { HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState } from "./hotkey-recorder.js";
10
10
  import { DEFAULT_SEQUENCE_TIMEOUT, HotkeySequence, SequenceManager, SequenceOptions, SequenceRegistrationHandle, SequenceRegistrationView, createSequenceMatcher, getSequenceManager } from "./sequence-manager.js";
11
11
  import { HotkeySequenceRecorder, HotkeySequenceRecorderCommitKeys, HotkeySequenceRecorderOptions, HotkeySequenceRecorderState } from "./hotkey-sequence-recorder.js";
12
12
  import { assertValidHotkey, checkHotkey, validateHotkey } from "./validate.js";
13
- export { ALL_KEYS, CanonicalModifier, ConflictBehavior, CreateHotkeyHandlerOptions, DEFAULT_SEQUENCE_TIMEOUT, EDITING_KEYS, EditingKey, FUNCTION_KEYS, FormatDisplayOptions, FormatKeyDebuggingOptions, FunctionKey, HeldKey, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyManager, HotkeyOptions, HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState, HotkeyRegistration, HotkeyRegistrationHandle, HotkeySequence, HotkeySequenceRecorder, HotkeySequenceRecorderCommitKeys, HotkeySequenceRecorderOptions, HotkeySequenceRecorderState, KEY_DISPLAY_SYMBOLS, Key, KeyStateTracker, KeyStateTrackerState, LETTER_KEYS, LetterKey, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, Modifier, NAVIGATION_KEYS, NUMBER_KEYS, NavigationKey, NumberKey, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, ParsedHotkey, PunctuationKey, RawHotkey, RegisterableHotkey, STANDARD_MODIFIER_LABELS, SequenceManager, SequenceOptions, SequenceRegistrationHandle, SequenceRegistrationView, ValidationResult, assertValidHotkey, checkHotkey, convertToModFormat, createHotkeyHandler, createMultiHotkeyHandler, createSequenceMatcher, detectPlatform, formatForDisplay, formatHotkey, formatHotkeySequence, formatKeyForDebuggingDisplay, formatWithLabels, getHotkeyManager, getKeyStateTracker, getSequenceManager, hasNonModifierKey, isModifier, isModifierKey, isSingleLetterKey, keyboardEventToHotkey, matchesKeyboardEvent, normalizeHotkey, normalizeKeyName, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey, resolveModifier, validateHotkey };
13
+ export { ALL_KEYS, CanonicalModifier, ConflictBehavior, CreateHotkeyHandlerOptions, DEFAULT_SEQUENCE_TIMEOUT, EDITING_KEYS, EditingKey, FUNCTION_KEYS, FormatDisplayOptions, FunctionKey, HeldKey, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyManager, HotkeyOptions, HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState, HotkeyRegistration, HotkeyRegistrationHandle, HotkeySequence, HotkeySequenceRecorder, HotkeySequenceRecorderCommitKeys, HotkeySequenceRecorderOptions, HotkeySequenceRecorderState, KEY_DISPLAY_SYMBOLS, Key, KeyStateTracker, KeyStateTrackerState, LETTER_KEYS, LINUX_MODIFIER_LABELS, LetterKey, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, Modifier, NAVIGATION_KEYS, NUMBER_KEYS, NavigationKey, NumberKey, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, PUNCTUATION_KEY_DISPLAY_LABELS, ParsedHotkey, PunctuationKey, RawHotkey, RegisterableHotkey, SequenceManager, SequenceOptions, SequenceRegistrationHandle, SequenceRegistrationView, ValidationResult, WINDOWS_MODIFIER_LABELS, assertValidHotkey, checkHotkey, createHotkeyHandler, createMultiHotkeyHandler, createSequenceMatcher, detectPlatform, formatForDisplay, formatHotkey, formatHotkeySequence, formatWithLabels, getHotkeyManager, getKeyStateTracker, getSequenceManager, hasNonModifierKey, isModifierKey, isSingleLetterKey, matchesKeyboardEvent, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeKeyName, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey, resolveModifier, validateHotkey };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, KEY_DISPLAY_SYMBOLS, LETTER_KEYS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, STANDARD_MODIFIER_LABELS, detectPlatform, isSingleLetterKey, normalizeKeyName, resolveModifier } from "./constants.js";
2
- import { convertToModFormat, hasNonModifierKey, isModifier, isModifierKey, keyboardEventToHotkey, normalizeHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey } from "./parse.js";
3
- import { formatForDisplay, formatHotkey, formatHotkeySequence, formatKeyForDebuggingDisplay, formatWithLabels } from "./format.js";
1
+ import { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, KEY_DISPLAY_SYMBOLS, LETTER_KEYS, LINUX_MODIFIER_LABELS, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, PUNCTUATION_KEY_DISPLAY_LABELS, WINDOWS_MODIFIER_LABELS, detectPlatform, isSingleLetterKey, normalizeKeyName, resolveModifier } from "./constants.js";
2
+ import { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey } from "./parse.js";
3
+ import { formatForDisplay, formatHotkey, formatHotkeySequence, formatWithLabels } from "./format.js";
4
4
  import { createHotkeyHandler, createMultiHotkeyHandler, matchesKeyboardEvent } from "./match.js";
5
5
  import { HotkeyManager, getHotkeyManager } from "./hotkey-manager.js";
6
6
  import { KeyStateTracker, getKeyStateTracker } from "./key-state-tracker.js";
@@ -9,4 +9,4 @@ import { HotkeySequenceRecorder } from "./hotkey-sequence-recorder.js";
9
9
  import { DEFAULT_SEQUENCE_TIMEOUT, SequenceManager, createSequenceMatcher, getSequenceManager } from "./sequence-manager.js";
10
10
  import { assertValidHotkey, checkHotkey, validateHotkey } from "./validate.js";
11
11
 
12
- export { ALL_KEYS, DEFAULT_SEQUENCE_TIMEOUT, EDITING_KEYS, FUNCTION_KEYS, HotkeyManager, HotkeyRecorder, HotkeySequenceRecorder, KEY_DISPLAY_SYMBOLS, KeyStateTracker, LETTER_KEYS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, STANDARD_MODIFIER_LABELS, SequenceManager, assertValidHotkey, checkHotkey, convertToModFormat, createHotkeyHandler, createMultiHotkeyHandler, createSequenceMatcher, detectPlatform, formatForDisplay, formatHotkey, formatHotkeySequence, formatKeyForDebuggingDisplay, formatWithLabels, getHotkeyManager, getKeyStateTracker, getSequenceManager, hasNonModifierKey, isModifier, isModifierKey, isSingleLetterKey, keyboardEventToHotkey, matchesKeyboardEvent, normalizeHotkey, normalizeKeyName, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey, resolveModifier, validateHotkey };
12
+ export { ALL_KEYS, DEFAULT_SEQUENCE_TIMEOUT, EDITING_KEYS, FUNCTION_KEYS, HotkeyManager, HotkeyRecorder, HotkeySequenceRecorder, KEY_DISPLAY_SYMBOLS, KeyStateTracker, LETTER_KEYS, LINUX_MODIFIER_LABELS, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, PUNCTUATION_KEY_DISPLAY_LABELS, SequenceManager, WINDOWS_MODIFIER_LABELS, assertValidHotkey, checkHotkey, createHotkeyHandler, createMultiHotkeyHandler, createSequenceMatcher, detectPlatform, formatForDisplay, formatHotkey, formatHotkeySequence, formatWithLabels, getHotkeyManager, getKeyStateTracker, getSequenceManager, hasNonModifierKey, isModifierKey, isSingleLetterKey, matchesKeyboardEvent, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeKeyName, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey, resolveModifier, validateHotkey };
package/dist/parse.cjs CHANGED
@@ -87,39 +87,67 @@ function rawHotkeyToParsedHotkey(raw, platform = require_constants.detectPlatfor
87
87
  };
88
88
  }
89
89
  /**
90
+ * Serializes a parsed hotkey to the canonical string form used for registration
91
+ * and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;
92
+ * otherwise full `Control+Alt+Shift+Meta` order.
93
+ */
94
+ function normalizedHotkeyStringFromParsed(parsed, platform) {
95
+ const canUseMod = platform === "mac" ? parsed.meta && !parsed.ctrl : parsed.ctrl && !parsed.meta;
96
+ const parts = [];
97
+ if (canUseMod) {
98
+ parts.push("Mod");
99
+ if (parsed.alt) parts.push("Alt");
100
+ if (parsed.shift) parts.push("Shift");
101
+ } else for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
102
+ parts.push(require_constants.normalizeKeyName(parsed.key));
103
+ return parts.join("+");
104
+ }
105
+ /**
90
106
  * Normalizes a hotkey string to its canonical form.
91
107
  *
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)
108
+ * - When `Mod` is allowed for the platform (Command on Mac without Control;
109
+ * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
110
+ * then the key (e.g. `Mod+Shift+E`, `Mod+S`).
111
+ * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
112
+ * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
97
113
  *
98
114
  * @param hotkey - The hotkey string to normalize
99
- * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
115
+ * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
100
116
  * @returns The normalized hotkey string
101
117
  *
102
118
  * @example
103
119
  * ```ts
104
- * normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'
105
- * normalizeHotkey('ctrl+a') // 'Control+A'
120
+ * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
121
+ * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
106
122
  * normalizeHotkey('esc') // 'Escape'
107
123
  * ```
108
124
  */
109
125
  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("+");
126
+ return normalizedHotkeyStringFromParsed(parseHotkey(hotkey, platform), platform);
127
+ }
128
+ /**
129
+ * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.
130
+ */
131
+ function normalizeHotkeyFromParsed(parsed, platform = require_constants.detectPlatform()) {
132
+ return normalizedHotkeyStringFromParsed(parsed, platform);
115
133
  }
116
134
  /**
117
- * Checks if a string represents a modifier key.
135
+ * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.
136
+ * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
137
+ */
138
+ function normalizeRegisterableHotkey(hotkey, platform = require_constants.detectPlatform()) {
139
+ return typeof hotkey === "string" ? normalizeHotkey(hotkey, platform) : normalizeHotkeyFromParsed(rawHotkeyToParsedHotkey(hotkey, platform), platform);
140
+ }
141
+ /**
142
+ * Checks if a string is a recognized modifier token (including aliases).
118
143
  *
119
- * @param key - The string to check
144
+ * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM
145
+ * spellings like `OS` / `Win` match the same alias table.
146
+ *
147
+ * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)
120
148
  * @returns True if the string is a recognized modifier
121
149
  */
122
- function isModifier(key) {
150
+ function isModifierKey(key) {
123
151
  return key in require_constants.MODIFIER_ALIASES || key.toLowerCase() in require_constants.MODIFIER_ALIASES;
124
152
  }
125
153
  /**
@@ -157,56 +185,13 @@ function parseKeyboardEvent(event) {
157
185
  };
158
186
  }
159
187
  /**
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
+ * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
188
189
  *
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
- * ```
190
+ * @param event - The keyboard event (typically `keydown`)
191
+ * @param platform - Target platform for `Mod` eligibility
206
192
  */
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";
193
+ function normalizeHotkeyFromEvent(event, platform = require_constants.detectPlatform()) {
194
+ return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform);
210
195
  }
211
196
  /**
212
197
  * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
@@ -227,39 +212,16 @@ function isModifierKey(event) {
227
212
  */
228
213
  function hasNonModifierKey(hotkey, platform = require_constants.detectPlatform()) {
229
214
  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;
215
+ return !isModifierKey(parsed.key) && parsed.key.length > 0;
254
216
  }
255
217
 
256
218
  //#endregion
257
- exports.convertToModFormat = convertToModFormat;
258
219
  exports.hasNonModifierKey = hasNonModifierKey;
259
- exports.isModifier = isModifier;
260
220
  exports.isModifierKey = isModifierKey;
261
- exports.keyboardEventToHotkey = keyboardEventToHotkey;
262
221
  exports.normalizeHotkey = normalizeHotkey;
222
+ exports.normalizeHotkeyFromEvent = normalizeHotkeyFromEvent;
223
+ exports.normalizeHotkeyFromParsed = normalizeHotkeyFromParsed;
224
+ exports.normalizeRegisterableHotkey = normalizeRegisterableHotkey;
263
225
  exports.parseHotkey = parseHotkey;
264
226
  exports.parseKeyboardEvent = parseKeyboardEvent;
265
227
  exports.rawHotkeyToParsedHotkey = rawHotkeyToParsedHotkey;
@@ -1 +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":";;;;;;;;;;;;;;;;;AA6BA,SAAgB,YACd,QACA,WAAwCA,kCAAgB,EAC1C;CACd,MAAM,QAAQ,OAAO,MAAM,IAAI;CAC/B,MAAM,4BAAoC,IAAI,KAAK;CACnD,IAAI,MAAM;AAEV,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;EACrC,MAAM,OAAO,MAAM,GAAI,MAAM;AAE7B,MAAI,MAAM,MAAM,SAAS,EAEvB,OAAMC,mCAAiB,KAAK;OACvB;GAEL,MAAM,QACJC,mCAAiB,SAASA,mCAAiB,KAAK,aAAa;AAE/D,OAAI,OAAO;IACT,MAAM,WAAWC,kCAAgB,OAAO,SAAS;AACjD,cAAU,IAAI,SAAS;cAInB,MAAM,WAAW,EACnB,OAAMF,mCAAiB,KAAK;;;AAOpC,KAAI,CAAC,OAAO,MAAM,SAAS,EACzB,OAAMA,mCAAiB,MAAM,MAAM,SAAS,GAAI,MAAM,CAAC;AAGzD,QAAO;EACL;EACA,MAAM,UAAU,IAAI,UAAU;EAC9B,OAAO,UAAU,IAAI,QAAQ;EAC7B,KAAK,UAAU,IAAI,MAAM;EACzB,MAAM,UAAU,IAAI,OAAO;EAC3B,WAAWG,iCAAe,QAAQ,MAAM,UAAU,IAAI,EAAE,CAAC;EAC1D;;;;;;;;;;;;;;;;;;;;;;;AAwBH,SAAgB,wBACd,KACA,WAAwCJ,kCAAgB,EAC1C;CACd,IAAI,OAAO,IAAI,QAAQ;CACvB,MAAM,QAAQ,IAAI,SAAS;CAC3B,MAAM,MAAM,IAAI,OAAO;CACvB,IAAI,OAAO,IAAI,QAAQ;AAEvB,KAAI,IAAI,IAEN,KADiBG,kCAAgB,OAAO,SAAS,KAChC,UACf,QAAO;KAEP,QAAO;CAIX,MAAM,YAAsCC,iCAAe,QAAQ,MAAM;AACvE,UAAQ,GAAR;GACE,KAAK,UACH,QAAO;GACT,KAAK,QACH,QAAO;GACT,KAAK,MACH,QAAO;GACT,KAAK,OACH,QAAO;GACT,QACE,QAAO;;GAEX;AACF,QAAO;EACL,KAAK,IAAI;EACT;EACA;EACA;EACA;EACA;EACD;;;;;;;;;;;;;;;;;;;;;;AAuBH,SAAgB,gBACd,QACA,WAAwCJ,kCAAgB,EAChD;CACR,MAAM,SAAS,YAAY,QAAQ,SAAS;CAC5C,MAAM,QAAuB,EAAE;AAG/B,MAAK,MAAM,YAAYI,iCACrB,KAAI,OAAO,UAAU,SAAS,SAAS,CACrC,OAAM,KAAK,SAAS;AAKxB,OAAM,KAAK,OAAO,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI;;;;;;;;AASxB,SAAgB,WAAW,KAAsB;AAC/C,QAAO,OAAOF,sCAAoB,IAAI,aAAa,IAAIA;;;;;;;;;;;;;;;;;;;;AAqBzD,SAAgB,mBAAmB,OAAoC;CACrE,MAAM,gBAAgBD,mCAAiB,MAAM,IAAI;CAGjD,MAAM,YAAsC,EAAE;AAC9C,KAAI,MAAM,QAAS,WAAU,KAAK,UAAU;AAC5C,KAAI,MAAM,OAAQ,WAAU,KAAK,MAAM;AACvC,KAAI,MAAM,SAAU,WAAU,KAAK,QAAQ;AAC3C,KAAI,MAAM,QAAS,WAAU,KAAK,OAAO;AAEzC,QAAO;EACL,KAAK;EACL,MAAM,MAAM;EACZ,OAAO,MAAM;EACb,KAAK,MAAM;EACX,MAAM,MAAM;EACZ;EACD;;;;;;;;;;;;;;;;;;;;;;AAuBH,SAAgB,sBAAsB,OAA8B;CAClE,MAAM,SAAS,mBAAmB,MAAM;CAGxC,MAAM,QAAuB,EAAE;AAC/B,MAAK,MAAM,YAAYG,iCACrB,KAAI,OAAO,UAAU,SAAS,SAAS,CACrC,OAAM,KAAK,SAAS;AAGxB,OAAM,KAAK,OAAO,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI;;;;;;;;;;;;;;;;;;;;;;;AAwBxB,SAAgB,cAAc,OAA+B;CAC3D,MAAM,MAAM,MAAM;AAClB,QACE,QAAQ,aACR,QAAQ,WACR,QAAQ,SACR,QAAQ,UACR,QAAQ,aACR,QAAQ,QACR,QAAQ;;;;;;;;;;;;;;;;;;;AAqBZ,SAAgB,kBACd,QACA,WAAwCJ,kCAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAM/D,QAAO,CAHe,WAAW,OAAO,IAAI,IAGnB,OAAO,IAAI,SAAS;;;;;;;;;;;;;;;;;;;AAoB/C,SAAgB,mBACd,QACA,WAAwCA,kCAAgB,EAChD;CACR,MAAM,SAAS,YAAY,QAAQ,SAAS;AAG5C,KAAI,aAAa,SAAS,OAAO,QAAQ,CAAC,OAAO,KAG/C,QADc,OAAO,MAAM,IAAI,CAE5B,KAAK,SAAU,SAAS,SAAS,QAAQ,KAAM,CAC/C,KAAK,IAAI;UACH,aAAa,SAAS,OAAO,QAAQ,CAAC,OAAO,KAGtD,QADc,OAAO,MAAM,IAAI,CAE5B,KAAK,SAAU,SAAS,YAAY,QAAQ,KAAM,CAClD,KAAK,IAAI;AAId,QAAO"}
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 RegisterableHotkey,\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 * Serializes a parsed hotkey to the canonical string form used for registration\n * and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;\n * otherwise full `Control+Alt+Shift+Meta` order.\n */\nfunction normalizedHotkeyStringFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey {\n const canUseMod =\n platform === 'mac'\n ? parsed.meta && !parsed.ctrl\n : parsed.ctrl && !parsed.meta\n\n const parts: Array<string> = []\n\n if (canUseMod) {\n parts.push('Mod')\n if (parsed.alt) {\n parts.push('Alt')\n }\n if (parsed.shift) {\n parts.push('Shift')\n }\n } else {\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n }\n\n parts.push(normalizeKeyName(parsed.key))\n return parts.join('+') as Hotkey\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * - When `Mod` is allowed for the platform (Command on Mac without Control;\n * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,\n * then the key (e.g. `Mod+Shift+E`, `Mod+S`).\n * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.\n * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).\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('shift+meta+e', 'mac') // 'Mod+Shift+E'\n * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.\n */\nexport function normalizeHotkeyFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parsed, platform)\n}\n\n/**\n * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.\n * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.\n */\nexport function normalizeRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return typeof hotkey === 'string'\n ? normalizeHotkey(hotkey, platform)\n : normalizeHotkeyFromParsed(\n rawHotkeyToParsedHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Checks if a string is a recognized modifier token (including aliases).\n *\n * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM\n * spellings like `OS` / `Win` match the same alias table.\n *\n * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)\n * @returns True if the string is a recognized modifier\n */\nexport function isModifierKey(\n key: Key | (string & {}),\n): key is keyof typeof MODIFIER_ALIASES {\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 * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.\n *\n * @param event - The keyboard event (typically `keydown`)\n * @param platform - Target platform for `Mod` eligibility\n */\nexport function normalizeHotkeyFromEvent(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform)\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 = isModifierKey(parsed.key)\n\n // A valid hotkey must have a non-modifier key\n return !keyIsModifier && parsed.key.length > 0\n}\n"],"mappings":"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"}
package/dist/parse.d.cts CHANGED
@@ -1,4 +1,5 @@
1
- import { Hotkey, Key, ParsedHotkey, RawHotkey } from "./hotkey.cjs";
1
+ import { Hotkey, Key, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.cjs";
2
+ import { MODIFIER_ALIASES } from "./constants.cjs";
2
3
 
3
4
  //#region src/parse.d.ts
4
5
  /**
@@ -41,31 +42,43 @@ declare function rawHotkeyToParsedHotkey(raw: RawHotkey, platform?: 'mac' | 'win
41
42
  /**
42
43
  * Normalizes a hotkey string to its canonical form.
43
44
  *
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)
45
+ * - When `Mod` is allowed for the platform (Command on Mac without Control;
46
+ * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
47
+ * then the key (e.g. `Mod+Shift+E`, `Mod+S`).
48
+ * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
49
+ * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
49
50
  *
50
51
  * @param hotkey - The hotkey string to normalize
51
- * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
52
+ * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
52
53
  * @returns The normalized hotkey string
53
54
  *
54
55
  * @example
55
56
  * ```ts
56
- * normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'
57
- * normalizeHotkey('ctrl+a') // 'Control+A'
57
+ * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
58
+ * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
58
59
  * normalizeHotkey('esc') // 'Escape'
59
60
  * ```
60
61
  */
61
- declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'): string;
62
+ declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'): Hotkey;
63
+ /**
64
+ * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.
65
+ */
66
+ declare function normalizeHotkeyFromParsed(parsed: ParsedHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
67
+ /**
68
+ * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.
69
+ * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
70
+ */
71
+ declare function normalizeRegisterableHotkey(hotkey: RegisterableHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
62
72
  /**
63
- * Checks if a string represents a modifier key.
73
+ * Checks if a string is a recognized modifier token (including aliases).
74
+ *
75
+ * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM
76
+ * spellings like `OS` / `Win` match the same alias table.
64
77
  *
65
- * @param key - The string to check
78
+ * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)
66
79
  * @returns True if the string is a recognized modifier
67
80
  */
68
- declare function isModifier(key: string): boolean;
81
+ declare function isModifierKey(key: Key | (string & {})): key is keyof typeof MODIFIER_ALIASES;
69
82
  /**
70
83
  * Parses a KeyboardEvent into a ParsedHotkey object.
71
84
  *
@@ -86,48 +99,12 @@ declare function isModifier(key: string): boolean;
86
99
  */
87
100
  declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
88
101
  /**
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')
102
+ * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
98
103
  *
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
- * ```
104
+ * @param event - The keyboard event (typically `keydown`)
105
+ * @param platform - Target platform for `Mod` eligibility
107
106
  */
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;
107
+ declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
131
108
  /**
132
109
  * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
133
110
  *
@@ -146,24 +123,6 @@ declare function isModifierKey(event: KeyboardEvent): boolean;
146
123
  * ```
147
124
  */
148
125
  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
126
  //#endregion
168
- export { convertToModFormat, hasNonModifierKey, isModifier, isModifierKey, keyboardEventToHotkey, normalizeHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
127
+ export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
169
128
  //# sourceMappingURL=parse.d.cts.map
package/dist/parse.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Hotkey, Key, ParsedHotkey, RawHotkey } from "./hotkey.js";
1
+ import { Hotkey, Key, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.js";
2
+ import { MODIFIER_ALIASES } from "./constants.js";
2
3
 
3
4
  //#region src/parse.d.ts
4
5
  /**
@@ -41,31 +42,43 @@ declare function rawHotkeyToParsedHotkey(raw: RawHotkey, platform?: 'mac' | 'win
41
42
  /**
42
43
  * Normalizes a hotkey string to its canonical form.
43
44
  *
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)
45
+ * - When `Mod` is allowed for the platform (Command on Mac without Control;
46
+ * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
47
+ * then the key (e.g. `Mod+Shift+E`, `Mod+S`).
48
+ * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
49
+ * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
49
50
  *
50
51
  * @param hotkey - The hotkey string to normalize
51
- * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
52
+ * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
52
53
  * @returns The normalized hotkey string
53
54
  *
54
55
  * @example
55
56
  * ```ts
56
- * normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'
57
- * normalizeHotkey('ctrl+a') // 'Control+A'
57
+ * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
58
+ * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
58
59
  * normalizeHotkey('esc') // 'Escape'
59
60
  * ```
60
61
  */
61
- declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'): string;
62
+ declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'): Hotkey;
63
+ /**
64
+ * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.
65
+ */
66
+ declare function normalizeHotkeyFromParsed(parsed: ParsedHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
67
+ /**
68
+ * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.
69
+ * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
70
+ */
71
+ declare function normalizeRegisterableHotkey(hotkey: RegisterableHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
62
72
  /**
63
- * Checks if a string represents a modifier key.
73
+ * Checks if a string is a recognized modifier token (including aliases).
74
+ *
75
+ * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM
76
+ * spellings like `OS` / `Win` match the same alias table.
64
77
  *
65
- * @param key - The string to check
78
+ * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)
66
79
  * @returns True if the string is a recognized modifier
67
80
  */
68
- declare function isModifier(key: string): boolean;
81
+ declare function isModifierKey(key: Key | (string & {})): key is keyof typeof MODIFIER_ALIASES;
69
82
  /**
70
83
  * Parses a KeyboardEvent into a ParsedHotkey object.
71
84
  *
@@ -86,48 +99,12 @@ declare function isModifier(key: string): boolean;
86
99
  */
87
100
  declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
88
101
  /**
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')
102
+ * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
98
103
  *
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
- * ```
104
+ * @param event - The keyboard event (typically `keydown`)
105
+ * @param platform - Target platform for `Mod` eligibility
107
106
  */
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;
107
+ declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
131
108
  /**
132
109
  * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
133
110
  *
@@ -146,24 +123,6 @@ declare function isModifierKey(event: KeyboardEvent): boolean;
146
123
  * ```
147
124
  */
148
125
  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
126
  //#endregion
168
- export { convertToModFormat, hasNonModifierKey, isModifier, isModifierKey, keyboardEventToHotkey, normalizeHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
127
+ export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
169
128
  //# sourceMappingURL=parse.d.ts.map