@tanstack/hotkeys 0.8.0 → 0.9.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 (158) hide show
  1. package/README.md +15 -1
  2. package/dist/{manager.utils.js → _event-target.cjs} +5 -41
  3. package/dist/_event-target.cjs.map +1 -0
  4. package/dist/{manager.utils.cjs → _event-target.js} +3 -47
  5. package/dist/_event-target.js.map +1 -0
  6. package/dist/_keyboard-event.cjs +83 -0
  7. package/dist/_keyboard-event.cjs.map +1 -0
  8. package/dist/_keyboard-event.js +76 -0
  9. package/dist/_keyboard-event.js.map +1 -0
  10. package/dist/_match.cjs +49 -0
  11. package/dist/_match.cjs.map +1 -0
  12. package/dist/_match.js +49 -0
  13. package/dist/_match.js.map +1 -0
  14. package/dist/_named-keys.cjs +68 -0
  15. package/dist/_named-keys.cjs.map +1 -0
  16. package/dist/_named-keys.d.cts +8 -0
  17. package/dist/_named-keys.d.ts +8 -0
  18. package/dist/_named-keys.js +66 -0
  19. package/dist/_named-keys.js.map +1 -0
  20. package/dist/_recorder-chord.cjs +44 -0
  21. package/dist/_recorder-chord.cjs.map +1 -0
  22. package/dist/_recorder-chord.js +43 -0
  23. package/dist/_recorder-chord.js.map +1 -0
  24. package/dist/_recording-guard.cjs +59 -0
  25. package/dist/_recording-guard.cjs.map +1 -0
  26. package/dist/_recording-guard.js +56 -0
  27. package/dist/_recording-guard.js.map +1 -0
  28. package/dist/_registration.cjs +54 -0
  29. package/dist/_registration.cjs.map +1 -0
  30. package/dist/_registration.js +50 -0
  31. package/dist/_registration.js.map +1 -0
  32. package/dist/conflicts.cjs +55 -0
  33. package/dist/conflicts.cjs.map +1 -0
  34. package/dist/conflicts.d.cts +39 -0
  35. package/dist/conflicts.d.ts +39 -0
  36. package/dist/conflicts.js +55 -0
  37. package/dist/conflicts.js.map +1 -0
  38. package/dist/constants.cjs +46 -171
  39. package/dist/constants.cjs.map +1 -1
  40. package/dist/constants.d.cts +4 -129
  41. package/dist/constants.d.ts +4 -129
  42. package/dist/constants.js +48 -164
  43. package/dist/constants.js.map +1 -1
  44. package/dist/display-labels.cjs +119 -0
  45. package/dist/display-labels.cjs.map +1 -0
  46. package/dist/display-labels.d.cts +93 -0
  47. package/dist/display-labels.d.ts +93 -0
  48. package/dist/display-labels.js +113 -0
  49. package/dist/display-labels.js.map +1 -0
  50. package/dist/format.cjs +30 -36
  51. package/dist/format.cjs.map +1 -1
  52. package/dist/format.d.cts +46 -7
  53. package/dist/format.d.ts +46 -7
  54. package/dist/format.js +31 -37
  55. package/dist/format.js.map +1 -1
  56. package/dist/hint.cjs +24 -0
  57. package/dist/hint.cjs.map +1 -0
  58. package/dist/hint.d.cts +23 -0
  59. package/dist/hint.d.ts +23 -0
  60. package/dist/hint.js +24 -0
  61. package/dist/hint.js.map +1 -0
  62. package/dist/hotkey-manager.cjs +53 -22
  63. package/dist/hotkey-manager.cjs.map +1 -1
  64. package/dist/hotkey-manager.d.cts +6 -2
  65. package/dist/hotkey-manager.d.ts +6 -2
  66. package/dist/hotkey-manager.js +49 -18
  67. package/dist/hotkey-manager.js.map +1 -1
  68. package/dist/hotkey-recorder.cjs +66 -10
  69. package/dist/hotkey-recorder.cjs.map +1 -1
  70. package/dist/hotkey-recorder.d.cts +5 -2
  71. package/dist/hotkey-recorder.d.ts +5 -2
  72. package/dist/hotkey-recorder.js +65 -9
  73. package/dist/hotkey-recorder.js.map +1 -1
  74. package/dist/hotkey-sequence-recorder.cjs +85 -22
  75. package/dist/hotkey-sequence-recorder.cjs.map +1 -1
  76. package/dist/hotkey-sequence-recorder.d.cts +9 -2
  77. package/dist/hotkey-sequence-recorder.d.ts +9 -2
  78. package/dist/hotkey-sequence-recorder.js +84 -21
  79. package/dist/hotkey-sequence-recorder.js.map +1 -1
  80. package/dist/{hotkey.d.ts → hotkey.types.d.cts} +55 -120
  81. package/dist/{hotkey.d.cts → hotkey.types.d.ts} +55 -120
  82. package/dist/index.cjs +18 -10
  83. package/dist/index.d.cts +14 -9
  84. package/dist/index.d.ts +14 -9
  85. package/dist/index.js +10 -6
  86. package/dist/key-state-tracker.cjs +24 -24
  87. package/dist/key-state-tracker.cjs.map +1 -1
  88. package/dist/key-state-tracker.js +24 -24
  89. package/dist/key-state-tracker.js.map +1 -1
  90. package/dist/key.types.d.cts +82 -0
  91. package/dist/key.types.d.ts +82 -0
  92. package/dist/match.cjs +21 -30
  93. package/dist/match.cjs.map +1 -1
  94. package/dist/match.d.cts +19 -3
  95. package/dist/match.d.ts +19 -3
  96. package/dist/match.js +19 -29
  97. package/dist/match.js.map +1 -1
  98. package/dist/parse.cjs +43 -26
  99. package/dist/parse.cjs.map +1 -1
  100. package/dist/parse.d.cts +10 -4
  101. package/dist/parse.d.ts +10 -4
  102. package/dist/parse.js +35 -19
  103. package/dist/parse.js.map +1 -1
  104. package/dist/platform.cjs +55 -0
  105. package/dist/platform.cjs.map +1 -0
  106. package/dist/platform.d.cts +44 -0
  107. package/dist/platform.d.ts +44 -0
  108. package/dist/platform.js +53 -0
  109. package/dist/platform.js.map +1 -0
  110. package/dist/recorder-options.d.cts +34 -0
  111. package/dist/recorder-options.d.ts +34 -0
  112. package/dist/sequence-manager.cjs +54 -28
  113. package/dist/sequence-manager.cjs.map +1 -1
  114. package/dist/sequence-manager.d.cts +18 -3
  115. package/dist/sequence-manager.d.ts +18 -3
  116. package/dist/sequence-manager.js +49 -23
  117. package/dist/sequence-manager.js.map +1 -1
  118. package/dist/validate.cjs +6 -10
  119. package/dist/validate.cjs.map +1 -1
  120. package/dist/validate.d.cts +13 -2
  121. package/dist/validate.d.ts +13 -2
  122. package/dist/validate.js +7 -11
  123. package/dist/validate.js.map +1 -1
  124. package/package.json +1 -1
  125. package/src/{manager.utils.ts → _event-target.ts} +0 -74
  126. package/src/_keyboard-event.ts +101 -0
  127. package/src/_match.ts +96 -0
  128. package/src/_named-keys.ts +62 -0
  129. package/src/_recorder-chord.ts +76 -0
  130. package/src/_recording-guard.ts +63 -0
  131. package/src/_registration.ts +79 -0
  132. package/src/conflicts.ts +125 -0
  133. package/src/constants.ts +48 -196
  134. package/src/display-labels.ts +123 -0
  135. package/src/format.ts +111 -52
  136. package/src/hint.ts +42 -0
  137. package/src/hotkey-manager.ts +95 -40
  138. package/src/hotkey-recorder.ts +91 -15
  139. package/src/hotkey-sequence-recorder.ts +110 -28
  140. package/src/{hotkey.ts → hotkey.types.ts} +57 -224
  141. package/src/index.ts +11 -5
  142. package/src/key-state-tracker.ts +36 -24
  143. package/src/key.types.ts +268 -0
  144. package/src/match.ts +41 -90
  145. package/src/parse.ts +67 -26
  146. package/src/platform.ts +66 -0
  147. package/src/recorder-options.ts +34 -0
  148. package/src/sequence-manager.ts +77 -42
  149. package/src/validate.ts +29 -21
  150. package/dist/manager.utils.cjs.map +0 -1
  151. package/dist/manager.utils.d.cts +0 -13
  152. package/dist/manager.utils.d.ts +0 -13
  153. package/dist/manager.utils.js.map +0 -1
  154. package/dist/recorder-chord.cjs +0 -18
  155. package/dist/recorder-chord.cjs.map +0 -1
  156. package/dist/recorder-chord.js +0 -18
  157. package/dist/recorder-chord.js.map +0 -1
  158. package/src/recorder-chord.ts +0 -25
@@ -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 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":";;;;;;;;;;;;;;;;;AA8BA,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,I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AAYP,SAAgB,cACd,KACsC;AACtC,QAAO,OAAOE,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;;;;;;;;AASH,SAAgB,yBACd,OACA,WAAwCD,kCAAgB,EAChD;AACR,QAAO,iCAAiC,mBAAmB,MAAM,EAAE,SAAS;;;;;;;;;;;;;;;;;;;AAoB9E,SAAgB,kBACd,QACA,WAAwCA,kCAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAM/D,QAAO,CAHe,cAAc,OAAO,IAAI,IAGtB,OAAO,IAAI,SAAS"}
1
+ {"version":3,"file":"parse.cjs","names":["detectPlatform","splitHotkeyParts","normalizeKeyName","MODIFIER_ALIASES","resolveModifier","MODIFIER_ORDER","normalizeKeyboardEvent"],"sources":["../src/parse.ts"],"sourcesContent":["import {\n assertLogicalKey,\n normalizeKeyboardEvent,\n splitHotkeyParts,\n} from './_keyboard-event'\nimport { MODIFIER_ALIASES, MODIFIER_ORDER, normalizeKeyName } from './constants'\nimport { detectPlatform, resolveModifier } from './platform'\nimport type { CanonicalModifier, Key } from './key.types'\nimport type {\n Hotkey,\n ParsedHotkey,\n RawHotkey,\n RegisterableHotkey,\n} from './hotkey.types'\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 with either logical key or physical code 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 = splitHotkeyParts(hotkey)\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 const code = /^\\[([A-Za-z][A-Za-z0-9]*)\\]$/.exec(key)?.[1]\n return {\n ...(code !== undefined ? { code } : { 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 if (raw.code === undefined) assertLogicalKey(raw.key)\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 ...(raw.code !== undefined\n ? { code: raw.code }\n : { key: normalizeKeyName(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 if (parsed.code === undefined) assertLogicalKey(parsed.key)\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(\n parsed.code !== undefined\n ? `[${parsed.code}]`\n : normalizeKeyName(parsed.key),\n )\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(\n event: KeyboardEvent,\n platform?: 'mac' | 'windows' | 'linux',\n): ParsedHotkey {\n const normalized = normalizeKeyboardEvent(event, platform)\n // AltGraph is a character-production modifier, not an ordinary Control+Alt\n // shortcut. Record the produced glyph instead of a synthetic Ctrl+Alt chord.\n const ctrl = normalized.altGraph ? false : normalized.ctrl\n const alt = normalized.altGraph ? false : normalized.alt\n // Shift is a real modifier even during AltGraph input; preserve it for replay.\n const shift = normalized.shift\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (ctrl) modifiers.push('Control')\n if (alt) modifiers.push('Alt')\n if (shift) modifiers.push('Shift')\n if (normalized.meta) modifiers.push('Meta')\n\n return {\n key: normalized.key,\n ctrl,\n shift,\n alt,\n meta: normalized.meta,\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(\n parseKeyboardEvent(event, platform),\n platform,\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 if (parsed.code !== undefined) {\n // Physical modifier codes identify a side, but still are not action keys.\n return (\n parsed.code.length > 0 &&\n !/^(Control|Shift|Alt|Meta)(Left|Right)$/.test(parsed.code)\n )\n }\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\n/**\n * Parses a string or raw binding, resolving Mod for the supplied platform.\n * Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.\n */\nexport function parseRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform = detectPlatform(),\n): ParsedHotkey {\n return typeof hotkey === 'string'\n ? parseHotkey(hotkey, platform)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;;;;;;;;;;;AAYP,SAAgB,cACd,KACsC;AACtC,QAAO,OAAOG,sCAAoB,IAAI,aAAa,IAAIA;;;;;;;;;;;;;;;;;;;;AAqBzD,SAAgB,mBACd,OACA,UACc;CACd,MAAM,aAAaG,+CAAuB,OAAO,SAAS;CAG1D,MAAM,OAAO,WAAW,WAAW,QAAQ,WAAW;CACtD,MAAM,MAAM,WAAW,WAAW,QAAQ,WAAW;CAErD,MAAM,QAAQ,WAAW;CAGzB,MAAM,YAAsC,EAAE;AAC9C,KAAI,KAAM,WAAU,KAAK,UAAU;AACnC,KAAI,IAAK,WAAU,KAAK,MAAM;AAC9B,KAAI,MAAO,WAAU,KAAK,QAAQ;AAClC,KAAI,WAAW,KAAM,WAAU,KAAK,OAAO;AAE3C,QAAO;EACL,KAAK,WAAW;EAChB;EACA;EACA;EACA,MAAM,WAAW;EACjB;EACD;;;;;;;;AASH,SAAgB,yBACd,OACA,WAAwCN,iCAAgB,EAChD;AACR,QAAO,iCACL,mBAAmB,OAAO,SAAS,EACnC,SACD;;;;;;;;;;;;;;;;;;;AAoBH,SAAgB,kBACd,QACA,WAAwCA,iCAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAE/D,KAAI,OAAO,SAAS,OAElB,QACE,OAAO,KAAK,SAAS,KACrB,CAAC,yCAAyC,KAAK,OAAO,KAAK;AAQ/D,QAAO,CAHe,cAAc,OAAO,IAAI,IAGtB,OAAO,IAAI,SAAS;;;;;;AAO/C,SAAgB,wBACd,QACA,WAAWA,iCAAgB,EACb;AACd,QAAO,OAAO,WAAW,WACrB,YAAY,QAAQ,SAAS,GAC7B,wBAAwB,QAAQ,SAAS"}
package/dist/parse.d.cts CHANGED
@@ -1,5 +1,6 @@
1
- import { Hotkey, Key, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.cjs";
1
+ import { Key } from "./key.types.cjs";
2
2
  import { MODIFIER_ALIASES } from "./constants.cjs";
3
+ import { Hotkey, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.types.cjs";
3
4
 
4
5
  //#region src/parse.d.ts
5
6
  /**
@@ -7,7 +8,7 @@ import { MODIFIER_ALIASES } from "./constants.cjs";
7
8
  *
8
9
  * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
9
10
  * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
10
- * @returns A ParsedHotkey object with the key and modifier flags
11
+ * @returns A ParsedHotkey with either logical key or physical code and modifier flags
11
12
  *
12
13
  * @example
13
14
  * ```ts
@@ -97,7 +98,7 @@ declare function isModifierKey(key: Key | (string & {})): key is keyof typeof MO
97
98
  * })
98
99
  * ```
99
100
  */
100
- declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
101
+ declare function parseKeyboardEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
101
102
  /**
102
103
  * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
103
104
  *
@@ -123,6 +124,11 @@ declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac'
123
124
  * ```
124
125
  */
125
126
  declare function hasNonModifierKey(hotkey: Hotkey | ParsedHotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): boolean;
127
+ /**
128
+ * Parses a string or raw binding, resolving Mod for the supplied platform.
129
+ * Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
130
+ */
131
+ declare function parseRegisterableHotkey(hotkey: RegisterableHotkey, platform?: "mac" | "windows" | "linux"): ParsedHotkey;
126
132
  //#endregion
127
- export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
133
+ export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, parseRegisterableHotkey, rawHotkeyToParsedHotkey };
128
134
  //# sourceMappingURL=parse.d.cts.map
package/dist/parse.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { Hotkey, Key, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.js";
1
+ import { Key } from "./key.types.js";
2
2
  import { MODIFIER_ALIASES } from "./constants.js";
3
+ import { Hotkey, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.types.js";
3
4
 
4
5
  //#region src/parse.d.ts
5
6
  /**
@@ -7,7 +8,7 @@ import { MODIFIER_ALIASES } from "./constants.js";
7
8
  *
8
9
  * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
9
10
  * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
10
- * @returns A ParsedHotkey object with the key and modifier flags
11
+ * @returns A ParsedHotkey with either logical key or physical code and modifier flags
11
12
  *
12
13
  * @example
13
14
  * ```ts
@@ -97,7 +98,7 @@ declare function isModifierKey(key: Key | (string & {})): key is keyof typeof MO
97
98
  * })
98
99
  * ```
99
100
  */
100
- declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
101
+ declare function parseKeyboardEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
101
102
  /**
102
103
  * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
103
104
  *
@@ -123,6 +124,11 @@ declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac'
123
124
  * ```
124
125
  */
125
126
  declare function hasNonModifierKey(hotkey: Hotkey | ParsedHotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): boolean;
127
+ /**
128
+ * Parses a string or raw binding, resolving Mod for the supplied platform.
129
+ * Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
130
+ */
131
+ declare function parseRegisterableHotkey(hotkey: RegisterableHotkey, platform?: "mac" | "windows" | "linux"): ParsedHotkey;
126
132
  //#endregion
127
- export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
133
+ export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, parseRegisterableHotkey, rawHotkeyToParsedHotkey };
128
134
  //# sourceMappingURL=parse.d.ts.map
package/dist/parse.js CHANGED
@@ -1,4 +1,6 @@
1
- import { MODIFIER_ALIASES, MODIFIER_ORDER, detectPlatform, normalizeKeyName, resolveModifier } from "./constants.js";
1
+ import { MODIFIER_ALIASES, MODIFIER_ORDER, normalizeKeyName } from "./constants.js";
2
+ import { detectPlatform, resolveModifier } from "./platform.js";
3
+ import { assertLogicalKey, normalizeKeyboardEvent, splitHotkeyParts } from "./_keyboard-event.js";
2
4
 
3
5
  //#region src/parse.ts
4
6
  /**
@@ -6,7 +8,7 @@ import { MODIFIER_ALIASES, MODIFIER_ORDER, detectPlatform, normalizeKeyName, res
6
8
  *
7
9
  * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
8
10
  * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
9
- * @returns A ParsedHotkey object with the key and modifier flags
11
+ * @returns A ParsedHotkey with either logical key or physical code and modifier flags
10
12
  *
11
13
  * @example
12
14
  * ```ts
@@ -16,7 +18,7 @@ import { MODIFIER_ALIASES, MODIFIER_ORDER, detectPlatform, normalizeKeyName, res
16
18
  * ```
17
19
  */
18
20
  function parseHotkey(hotkey, platform = detectPlatform()) {
19
- const parts = hotkey.split("+");
21
+ const parts = splitHotkeyParts(hotkey);
20
22
  const modifiers = /* @__PURE__ */ new Set();
21
23
  let key = "";
22
24
  for (let i = 0; i < parts.length; i++) {
@@ -31,8 +33,9 @@ function parseHotkey(hotkey, platform = detectPlatform()) {
31
33
  }
32
34
  }
33
35
  if (!key && parts.length > 0) key = normalizeKeyName(parts[parts.length - 1].trim());
36
+ const code = /^\[([A-Za-z][A-Za-z0-9]*)\]$/.exec(key)?.[1];
34
37
  return {
35
- key,
38
+ ...code !== void 0 ? { code } : { key },
36
39
  ctrl: modifiers.has("Control"),
37
40
  shift: modifiers.has("Shift"),
38
41
  alt: modifiers.has("Alt"),
@@ -62,6 +65,7 @@ function parseHotkey(hotkey, platform = detectPlatform()) {
62
65
  * ```
63
66
  */
64
67
  function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
68
+ if (raw.code === void 0) assertLogicalKey(raw.key);
65
69
  let ctrl = raw.ctrl ?? false;
66
70
  const shift = raw.shift ?? false;
67
71
  const alt = raw.alt ?? false;
@@ -78,7 +82,7 @@ function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
78
82
  }
79
83
  });
80
84
  return {
81
- key: raw.key,
85
+ ...raw.code !== void 0 ? { code: raw.code } : { key: normalizeKeyName(raw.key) },
82
86
  ctrl,
83
87
  shift,
84
88
  alt,
@@ -92,6 +96,7 @@ function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
92
96
  * otherwise full `Control+Alt+Shift+Meta` order.
93
97
  */
94
98
  function normalizedHotkeyStringFromParsed(parsed, platform) {
99
+ if (parsed.code === void 0) assertLogicalKey(parsed.key);
95
100
  const canUseMod = platform === "mac" ? parsed.meta && !parsed.ctrl : parsed.ctrl && !parsed.meta;
96
101
  const parts = [];
97
102
  if (canUseMod) {
@@ -99,7 +104,7 @@ function normalizedHotkeyStringFromParsed(parsed, platform) {
99
104
  if (parsed.alt) parts.push("Alt");
100
105
  if (parsed.shift) parts.push("Shift");
101
106
  } else for (const modifier of MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
102
- parts.push(normalizeKeyName(parsed.key));
107
+ parts.push(parsed.code !== void 0 ? `[${parsed.code}]` : normalizeKeyName(parsed.key));
103
108
  return parts.join("+");
104
109
  }
105
110
  /**
@@ -168,19 +173,22 @@ function isModifierKey(key) {
168
173
  * })
169
174
  * ```
170
175
  */
171
- function parseKeyboardEvent(event) {
172
- const normalizedKey = normalizeKeyName(event.key);
176
+ function parseKeyboardEvent(event, platform) {
177
+ const normalized = normalizeKeyboardEvent(event, platform);
178
+ const ctrl = normalized.altGraph ? false : normalized.ctrl;
179
+ const alt = normalized.altGraph ? false : normalized.alt;
180
+ const shift = normalized.shift;
173
181
  const modifiers = [];
174
- if (event.ctrlKey) modifiers.push("Control");
175
- if (event.altKey) modifiers.push("Alt");
176
- if (event.shiftKey) modifiers.push("Shift");
177
- if (event.metaKey) modifiers.push("Meta");
182
+ if (ctrl) modifiers.push("Control");
183
+ if (alt) modifiers.push("Alt");
184
+ if (shift) modifiers.push("Shift");
185
+ if (normalized.meta) modifiers.push("Meta");
178
186
  return {
179
- key: normalizedKey,
180
- ctrl: event.ctrlKey,
181
- shift: event.shiftKey,
182
- alt: event.altKey,
183
- meta: event.metaKey,
187
+ key: normalized.key,
188
+ ctrl,
189
+ shift,
190
+ alt,
191
+ meta: normalized.meta,
184
192
  modifiers
185
193
  };
186
194
  }
@@ -191,7 +199,7 @@ function parseKeyboardEvent(event) {
191
199
  * @param platform - Target platform for `Mod` eligibility
192
200
  */
193
201
  function normalizeHotkeyFromEvent(event, platform = detectPlatform()) {
194
- return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform);
202
+ return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event, platform), platform);
195
203
  }
196
204
  /**
197
205
  * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
@@ -212,9 +220,17 @@ function normalizeHotkeyFromEvent(event, platform = detectPlatform()) {
212
220
  */
213
221
  function hasNonModifierKey(hotkey, platform = detectPlatform()) {
214
222
  const parsed = typeof hotkey === "string" ? parseHotkey(hotkey, platform) : hotkey;
223
+ if (parsed.code !== void 0) return parsed.code.length > 0 && !/^(Control|Shift|Alt|Meta)(Left|Right)$/.test(parsed.code);
215
224
  return !isModifierKey(parsed.key) && parsed.key.length > 0;
216
225
  }
226
+ /**
227
+ * Parses a string or raw binding, resolving Mod for the supplied platform.
228
+ * Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
229
+ */
230
+ function parseRegisterableHotkey(hotkey, platform = detectPlatform()) {
231
+ return typeof hotkey === "string" ? parseHotkey(hotkey, platform) : rawHotkeyToParsedHotkey(hotkey, platform);
232
+ }
217
233
 
218
234
  //#endregion
219
- export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
235
+ export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, parseRegisterableHotkey, rawHotkeyToParsedHotkey };
220
236
  //# sourceMappingURL=parse.js.map
package/dist/parse.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"parse.js","names":[],"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":";;;;;;;;;;;;;;;;;AA8BA,SAAgB,YACd,QACA,WAAwC,gBAAgB,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,OAAM,iBAAiB,KAAK;OACvB;GAEL,MAAM,QACJ,iBAAiB,SAAS,iBAAiB,KAAK,aAAa;AAE/D,OAAI,OAAO;IACT,MAAM,WAAW,gBAAgB,OAAO,SAAS;AACjD,cAAU,IAAI,SAAS;cAInB,MAAM,WAAW,EACnB,OAAM,iBAAiB,KAAK;;;AAOpC,KAAI,CAAC,OAAO,MAAM,SAAS,EACzB,OAAM,iBAAiB,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,WAAW,eAAe,QAAQ,MAAM,UAAU,IAAI,EAAE,CAAC;EAC1D;;;;;;;;;;;;;;;;;;;;;;;AAwBH,SAAgB,wBACd,KACA,WAAwC,gBAAgB,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,KADiB,gBAAgB,OAAO,SAAS,KAChC,UACf,QAAO;KAEP,QAAO;CAIX,MAAM,YAAsC,eAAe,QAA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C,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;;;;;;;;AASH,SAAgB,yBACd,OACA,WAAwC,gBAAgB,EAChD;AACR,QAAO,iCAAiC,mBAAmB,MAAM,EAAE,SAAS;;;;;;;;;;;;;;;;;;;AAoB9E,SAAgB,kBACd,QACA,WAAwC,gBAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAM/D,QAAO,CAHe,cAAc,OAAO,IAAI,IAGtB,OAAO,IAAI,SAAS"}
1
+ {"version":3,"file":"parse.js","names":[],"sources":["../src/parse.ts"],"sourcesContent":["import {\n assertLogicalKey,\n normalizeKeyboardEvent,\n splitHotkeyParts,\n} from './_keyboard-event'\nimport { MODIFIER_ALIASES, MODIFIER_ORDER, normalizeKeyName } from './constants'\nimport { detectPlatform, resolveModifier } from './platform'\nimport type { CanonicalModifier, Key } from './key.types'\nimport type {\n Hotkey,\n ParsedHotkey,\n RawHotkey,\n RegisterableHotkey,\n} from './hotkey.types'\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 with either logical key or physical code 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 = splitHotkeyParts(hotkey)\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 const code = /^\\[([A-Za-z][A-Za-z0-9]*)\\]$/.exec(key)?.[1]\n return {\n ...(code !== undefined ? { code } : { 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 if (raw.code === undefined) assertLogicalKey(raw.key)\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 ...(raw.code !== undefined\n ? { code: raw.code }\n : { key: normalizeKeyName(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 if (parsed.code === undefined) assertLogicalKey(parsed.key)\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(\n parsed.code !== undefined\n ? `[${parsed.code}]`\n : normalizeKeyName(parsed.key),\n )\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(\n event: KeyboardEvent,\n platform?: 'mac' | 'windows' | 'linux',\n): ParsedHotkey {\n const normalized = normalizeKeyboardEvent(event, platform)\n // AltGraph is a character-production modifier, not an ordinary Control+Alt\n // shortcut. Record the produced glyph instead of a synthetic Ctrl+Alt chord.\n const ctrl = normalized.altGraph ? false : normalized.ctrl\n const alt = normalized.altGraph ? false : normalized.alt\n // Shift is a real modifier even during AltGraph input; preserve it for replay.\n const shift = normalized.shift\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (ctrl) modifiers.push('Control')\n if (alt) modifiers.push('Alt')\n if (shift) modifiers.push('Shift')\n if (normalized.meta) modifiers.push('Meta')\n\n return {\n key: normalized.key,\n ctrl,\n shift,\n alt,\n meta: normalized.meta,\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(\n parseKeyboardEvent(event, platform),\n platform,\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 if (parsed.code !== undefined) {\n // Physical modifier codes identify a side, but still are not action keys.\n return (\n parsed.code.length > 0 &&\n !/^(Control|Shift|Alt|Meta)(Left|Right)$/.test(parsed.code)\n )\n }\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\n/**\n * Parses a string or raw binding, resolving Mod for the supplied platform.\n * Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.\n */\nexport function parseRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform = detectPlatform(),\n): ParsedHotkey {\n return typeof hotkey === 'string'\n ? parseHotkey(hotkey, platform)\n : rawHotkeyToParsedHotkey(hotkey, platform)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,YACd,QACA,WAAwC,gBAAgB,EAC1C;CACd,MAAM,QAAQ,iBAAiB,OAAO;CACtC,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,OA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C,KAAI,IAAK,WAAU,KAAK,MAAM;AAC9B,KAAI,MAAO,WAAU,KAAK,QAAQ;AAClC,KAAI,WAAW,KAAM,WAAU,KAAK,OAAO;AAE3C,QAAO;EACL,KAAK,WAAW;EAChB;EACA;EACA;EACA,MAAM,WAAW;EACjB;EACD;;;;;;;;AASH,SAAgB,yBACd,OACA,WAAwC,gBAAgB,EAChD;AACR,QAAO,iCACL,mBAAmB,OAAO,SAAS,EACnC,SACD;;;;;;;;;;;;;;;;;;;AAoBH,SAAgB,kBACd,QACA,WAAwC,gBAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAE/D,KAAI,OAAO,SAAS,OAElB,QACE,OAAO,KAAK,SAAS,KACrB,CAAC,yCAAyC,KAAK,OAAO,KAAK;AAQ/D,QAAO,CAHe,cAAc,OAAO,IAAI,IAGtB,OAAO,IAAI,SAAS;;;;;;AAO/C,SAAgB,wBACd,QACA,WAAW,gBAAgB,EACb;AACd,QAAO,OAAO,WAAW,WACrB,YAAY,QAAQ,SAAS,GAC7B,wBAAwB,QAAQ,SAAS"}
@@ -0,0 +1,55 @@
1
+
2
+ //#region src/platform.ts
3
+ /**
4
+ * Detects the current platform based on browser navigator properties.
5
+ *
6
+ * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
7
+ * Control elsewhere) and for platform-specific hotkey formatting.
8
+ *
9
+ * @returns The detected platform: 'mac', 'windows', or 'linux'
10
+ * @remarks Defaults to 'linux' in SSR environments where navigator is undefined
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
15
+ * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
16
+ * ```
17
+ */
18
+ function detectPlatform() {
19
+ if (typeof navigator === "undefined") return "linux";
20
+ const platform = navigator.platform?.toLowerCase() ?? "";
21
+ const userAgent = navigator.userAgent?.toLowerCase() ?? "";
22
+ if (platform.includes("mac") || userAgent.includes("mac")) return "mac";
23
+ if (platform.includes("win") || userAgent.includes("win")) return "windows";
24
+ return "linux";
25
+ }
26
+ /**
27
+ * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
28
+ *
29
+ * The 'Mod' token represents the "primary modifier" on each platform:
30
+ * - macOS: 'Meta' (Command key ⌘)
31
+ * - Windows/Linux: 'Control' (Ctrl key)
32
+ *
33
+ * This enables cross-platform hotkey definitions like 'Mod+S' that automatically
34
+ * map to Command+S on Mac and Ctrl+S on Windows/Linux.
35
+ *
36
+ * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
37
+ * @param platform - The target platform. Defaults to auto-detection.
38
+ * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * resolveModifier('Mod', 'mac') // 'Meta'
43
+ * resolveModifier('Mod', 'windows') // 'Control'
44
+ * resolveModifier('Control', 'mac') // 'Control' (unchanged)
45
+ * ```
46
+ */
47
+ function resolveModifier(modifier, platform = detectPlatform()) {
48
+ if (modifier === "Mod") return platform === "mac" ? "Meta" : "Control";
49
+ return modifier;
50
+ }
51
+
52
+ //#endregion
53
+ exports.detectPlatform = detectPlatform;
54
+ exports.resolveModifier = resolveModifier;
55
+ //# sourceMappingURL=platform.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platform.cjs","names":[],"sources":["../src/platform.ts"],"sourcesContent":["import type { CanonicalModifier } from './key.types'\n\n/**\n * Detects the current platform based on browser navigator properties.\n *\n * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,\n * Control elsewhere) and for platform-specific hotkey formatting.\n *\n * @returns The detected platform: 'mac', 'windows', or 'linux'\n * @remarks Defaults to 'linux' in SSR environments where navigator is undefined\n *\n * @example\n * ```ts\n * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'\n * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere\n * ```\n */\nexport function detectPlatform(): 'mac' | 'windows' | 'linux' {\n if (typeof navigator === 'undefined') {\n return 'linux' // Default for SSR\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const platform = navigator.platform?.toLowerCase() ?? ''\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const userAgent = navigator.userAgent?.toLowerCase() ?? ''\n\n if (platform.includes('mac') || userAgent.includes('mac')) {\n return 'mac'\n }\n if (platform.includes('win') || userAgent.includes('win')) {\n return 'windows'\n }\n return 'linux'\n}\n\n/**\n * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.\n *\n * The 'Mod' token represents the \"primary modifier\" on each platform:\n * - macOS: 'Meta' (Command key ⌘)\n * - Windows/Linux: 'Control' (Ctrl key)\n *\n * This enables cross-platform hotkey definitions like 'Mod+S' that automatically\n * map to Command+S on Mac and Ctrl+S on Windows/Linux.\n *\n * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.\n * @param platform - The target platform. Defaults to auto-detection.\n * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')\n *\n * @example\n * ```ts\n * resolveModifier('Mod', 'mac') // 'Meta'\n * resolveModifier('Mod', 'windows') // 'Control'\n * resolveModifier('Control', 'mac') // 'Control' (unchanged)\n * ```\n */\nexport function resolveModifier(\n modifier: CanonicalModifier | 'Mod',\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): CanonicalModifier {\n if (modifier === 'Mod') {\n return platform === 'mac' ? 'Meta' : 'Control'\n }\n return modifier\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAiBA,SAAgB,iBAA8C;AAC5D,KAAI,OAAO,cAAc,YACvB,QAAO;CAIT,MAAM,WAAW,UAAU,UAAU,aAAa,IAAI;CAEtD,MAAM,YAAY,UAAU,WAAW,aAAa,IAAI;AAExD,KAAI,SAAS,SAAS,MAAM,IAAI,UAAU,SAAS,MAAM,CACvD,QAAO;AAET,KAAI,SAAS,SAAS,MAAM,IAAI,UAAU,SAAS,MAAM,CACvD,QAAO;AAET,QAAO;;;;;;;;;;;;;;;;;;;;;;;AAwBT,SAAgB,gBACd,UACA,WAAwC,gBAAgB,EACrC;AACnB,KAAI,aAAa,MACf,QAAO,aAAa,QAAQ,SAAS;AAEvC,QAAO"}
@@ -0,0 +1,44 @@
1
+ import { CanonicalModifier } from "./key.types.cjs";
2
+
3
+ //#region src/platform.d.ts
4
+ /**
5
+ * Detects the current platform based on browser navigator properties.
6
+ *
7
+ * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
8
+ * Control elsewhere) and for platform-specific hotkey formatting.
9
+ *
10
+ * @returns The detected platform: 'mac', 'windows', or 'linux'
11
+ * @remarks Defaults to 'linux' in SSR environments where navigator is undefined
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
16
+ * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
17
+ * ```
18
+ */
19
+ declare function detectPlatform(): 'mac' | 'windows' | 'linux';
20
+ /**
21
+ * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
22
+ *
23
+ * The 'Mod' token represents the "primary modifier" on each platform:
24
+ * - macOS: 'Meta' (Command key ⌘)
25
+ * - Windows/Linux: 'Control' (Ctrl key)
26
+ *
27
+ * This enables cross-platform hotkey definitions like 'Mod+S' that automatically
28
+ * map to Command+S on Mac and Ctrl+S on Windows/Linux.
29
+ *
30
+ * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
31
+ * @param platform - The target platform. Defaults to auto-detection.
32
+ * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * resolveModifier('Mod', 'mac') // 'Meta'
37
+ * resolveModifier('Mod', 'windows') // 'Control'
38
+ * resolveModifier('Control', 'mac') // 'Control' (unchanged)
39
+ * ```
40
+ */
41
+ declare function resolveModifier(modifier: CanonicalModifier | 'Mod', platform?: 'mac' | 'windows' | 'linux'): CanonicalModifier;
42
+ //#endregion
43
+ export { detectPlatform, resolveModifier };
44
+ //# sourceMappingURL=platform.d.cts.map
@@ -0,0 +1,44 @@
1
+ import { CanonicalModifier } from "./key.types.js";
2
+
3
+ //#region src/platform.d.ts
4
+ /**
5
+ * Detects the current platform based on browser navigator properties.
6
+ *
7
+ * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
8
+ * Control elsewhere) and for platform-specific hotkey formatting.
9
+ *
10
+ * @returns The detected platform: 'mac', 'windows', or 'linux'
11
+ * @remarks Defaults to 'linux' in SSR environments where navigator is undefined
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
16
+ * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
17
+ * ```
18
+ */
19
+ declare function detectPlatform(): 'mac' | 'windows' | 'linux';
20
+ /**
21
+ * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
22
+ *
23
+ * The 'Mod' token represents the "primary modifier" on each platform:
24
+ * - macOS: 'Meta' (Command key ⌘)
25
+ * - Windows/Linux: 'Control' (Ctrl key)
26
+ *
27
+ * This enables cross-platform hotkey definitions like 'Mod+S' that automatically
28
+ * map to Command+S on Mac and Ctrl+S on Windows/Linux.
29
+ *
30
+ * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
31
+ * @param platform - The target platform. Defaults to auto-detection.
32
+ * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * resolveModifier('Mod', 'mac') // 'Meta'
37
+ * resolveModifier('Mod', 'windows') // 'Control'
38
+ * resolveModifier('Control', 'mac') // 'Control' (unchanged)
39
+ * ```
40
+ */
41
+ declare function resolveModifier(modifier: CanonicalModifier | 'Mod', platform?: 'mac' | 'windows' | 'linux'): CanonicalModifier;
42
+ //#endregion
43
+ export { detectPlatform, resolveModifier };
44
+ //# sourceMappingURL=platform.d.ts.map
@@ -0,0 +1,53 @@
1
+ //#region src/platform.ts
2
+ /**
3
+ * Detects the current platform based on browser navigator properties.
4
+ *
5
+ * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
6
+ * Control elsewhere) and for platform-specific hotkey formatting.
7
+ *
8
+ * @returns The detected platform: 'mac', 'windows', or 'linux'
9
+ * @remarks Defaults to 'linux' in SSR environments where navigator is undefined
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
14
+ * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
15
+ * ```
16
+ */
17
+ function detectPlatform() {
18
+ if (typeof navigator === "undefined") return "linux";
19
+ const platform = navigator.platform?.toLowerCase() ?? "";
20
+ const userAgent = navigator.userAgent?.toLowerCase() ?? "";
21
+ if (platform.includes("mac") || userAgent.includes("mac")) return "mac";
22
+ if (platform.includes("win") || userAgent.includes("win")) return "windows";
23
+ return "linux";
24
+ }
25
+ /**
26
+ * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
27
+ *
28
+ * The 'Mod' token represents the "primary modifier" on each platform:
29
+ * - macOS: 'Meta' (Command key ⌘)
30
+ * - Windows/Linux: 'Control' (Ctrl key)
31
+ *
32
+ * This enables cross-platform hotkey definitions like 'Mod+S' that automatically
33
+ * map to Command+S on Mac and Ctrl+S on Windows/Linux.
34
+ *
35
+ * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
36
+ * @param platform - The target platform. Defaults to auto-detection.
37
+ * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * resolveModifier('Mod', 'mac') // 'Meta'
42
+ * resolveModifier('Mod', 'windows') // 'Control'
43
+ * resolveModifier('Control', 'mac') // 'Control' (unchanged)
44
+ * ```
45
+ */
46
+ function resolveModifier(modifier, platform = detectPlatform()) {
47
+ if (modifier === "Mod") return platform === "mac" ? "Meta" : "Control";
48
+ return modifier;
49
+ }
50
+
51
+ //#endregion
52
+ export { detectPlatform, resolveModifier };
53
+ //# sourceMappingURL=platform.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platform.js","names":[],"sources":["../src/platform.ts"],"sourcesContent":["import type { CanonicalModifier } from './key.types'\n\n/**\n * Detects the current platform based on browser navigator properties.\n *\n * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,\n * Control elsewhere) and for platform-specific hotkey formatting.\n *\n * @returns The detected platform: 'mac', 'windows', or 'linux'\n * @remarks Defaults to 'linux' in SSR environments where navigator is undefined\n *\n * @example\n * ```ts\n * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'\n * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere\n * ```\n */\nexport function detectPlatform(): 'mac' | 'windows' | 'linux' {\n if (typeof navigator === 'undefined') {\n return 'linux' // Default for SSR\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const platform = navigator.platform?.toLowerCase() ?? ''\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const userAgent = navigator.userAgent?.toLowerCase() ?? ''\n\n if (platform.includes('mac') || userAgent.includes('mac')) {\n return 'mac'\n }\n if (platform.includes('win') || userAgent.includes('win')) {\n return 'windows'\n }\n return 'linux'\n}\n\n/**\n * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.\n *\n * The 'Mod' token represents the \"primary modifier\" on each platform:\n * - macOS: 'Meta' (Command key ⌘)\n * - Windows/Linux: 'Control' (Ctrl key)\n *\n * This enables cross-platform hotkey definitions like 'Mod+S' that automatically\n * map to Command+S on Mac and Ctrl+S on Windows/Linux.\n *\n * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.\n * @param platform - The target platform. Defaults to auto-detection.\n * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')\n *\n * @example\n * ```ts\n * resolveModifier('Mod', 'mac') // 'Meta'\n * resolveModifier('Mod', 'windows') // 'Control'\n * resolveModifier('Control', 'mac') // 'Control' (unchanged)\n * ```\n */\nexport function resolveModifier(\n modifier: CanonicalModifier | 'Mod',\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): CanonicalModifier {\n if (modifier === 'Mod') {\n return platform === 'mac' ? 'Meta' : 'Control'\n }\n return modifier\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiBA,SAAgB,iBAA8C;AAC5D,KAAI,OAAO,cAAc,YACvB,QAAO;CAIT,MAAM,WAAW,UAAU,UAAU,aAAa,IAAI;CAEtD,MAAM,YAAY,UAAU,WAAW,aAAa,IAAI;AAExD,KAAI,SAAS,SAAS,MAAM,IAAI,UAAU,SAAS,MAAM,CACvD,QAAO;AAET,KAAI,SAAS,SAAS,MAAM,IAAI,UAAU,SAAS,MAAM,CACvD,QAAO;AAET,QAAO;;;;;;;;;;;;;;;;;;;;;;;AAwBT,SAAgB,gBACd,UACA,WAAwC,gBAAgB,EACrC;AACnB,KAAI,aAAa,MACf,QAAO,aAAa,QAAQ,SAAS;AAEvC,QAAO"}
@@ -0,0 +1,34 @@
1
+ import { Hotkey, ParsedHotkey } from "./hotkey.types.cjs";
2
+ import { HotkeyConflict, HotkeyConflictOptions } from "./conflicts.cjs";
3
+
4
+ //#region src/recorder-options.d.ts
5
+ /** Event property used to record a binding. Physical codes are the default. */
6
+ type RecorderKeyMode = 'code' | 'key';
7
+ interface RecorderRejection {
8
+ reason: 'missing-code' | 'alt-graph' | 'invalid' | 'conflict' | 'validation';
9
+ message: string;
10
+ hotkey?: Hotkey;
11
+ sequence?: Array<Hotkey>;
12
+ conflicts?: Array<HotkeyConflict>;
13
+ }
14
+ interface RecorderOptions {
15
+ /** Physical code recording is the default. Choose key to record logical characters. */
16
+ recordBy?: RecorderKeyMode;
17
+ /** Platform used for Mod and AltGraph handling. Defaults to platform detection. */
18
+ platform?: 'mac' | 'windows' | 'linux';
19
+ /** Opt in to live-registry conflicts, including sequence prefixes. Default: false. */
20
+ detectConflicts?: boolean | HotkeyConflictOptions;
21
+ /** Rejected candidates leave recording active. */
22
+ onReject?: (rejection: RecorderRejection) => void;
23
+ }
24
+ interface HotkeyRecorderValidationContext {
25
+ event: KeyboardEvent;
26
+ parsedHotkey: ParsedHotkey;
27
+ }
28
+ interface HotkeySequenceRecorderValidationContext {
29
+ events: ReadonlyArray<KeyboardEvent>;
30
+ parsedSequence: ReadonlyArray<ParsedHotkey>;
31
+ }
32
+ //#endregion
33
+ export { HotkeyRecorderValidationContext, HotkeySequenceRecorderValidationContext, RecorderKeyMode, RecorderOptions, RecorderRejection };
34
+ //# sourceMappingURL=recorder-options.d.cts.map