fix: add event.code fallback for Alt+punctuation keys on macOS #64
main fromrefs/pull/64/head·merged by KevinVandy Summary#
- On macOS, the Option (Alt) key acts as a character composer for punctuation keys (e.g.,
Option+-produces an en-dash–), causingevent.keyto differ from the expected character. This preventedAlt+punctuationhotkeys likeAlt+-,Alt+/,Alt+[, etc. from matching. - Added a
PUNCTUATION_CODE_MAPinconstants.tsthat mapsevent.codevalues (Minus,Equal,Slash,BracketLeft,BracketRight,Backslash,Comma,Period,Backquote,Semicolon) to their canonical characters, following the same fallback pattern already used for letter (Key*) and digit (Digit*) codes. - This also fixes dead-key + punctuation code matching (e.g.,
Alt+[whenevent.keyisDead).
Fixes the issue described in the bug report where all PUNCTUATION_KEYS combined with Alt on macOS failed to match.
Test plan#
- Added tests for all punctuation keys with Alt modifier (
Alt+-,Alt+=,Alt+/,Alt+[,Alt+],Alt+\,Alt+,,Alt+., `Alt+``) - Added test for multi-modifier combinations (
Mod+Alt+-on macOS) - Added test for direct punctuation matching without fallback (no regression)
- Added test for missing
event.codeedge case - Added test for dead key + punctuation code fallback
- Updated existing dead-key test to expect correct behavior
-
pnpm testpasses (all targets: eslint, types, lib, docs, build)
Summary by CodeRabbit#
Bug Fixes
- macOS Alt/Option + punctuation hotkeys now reliably trigger by using a physical-key fallback when the composed character differs.
New Features
- Semicolon (;) is now accepted as a valid punctuation key in hotkeys.
Documentation
- Reference docs updated to document punctuation fallback behavior and the new punctuation mapping; minor source-link updates across docs.
- 1
@tanstack/angular-hotkeys
npm i https://pkg.pr.new/@tanstack/angular-hotkeys@64@tanstack/hotkeys
npm i https://pkg.pr.new/@tanstack/hotkeys@64@tanstack/hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/hotkeys-devtools@64@tanstack/preact-hotkeys
npm i https://pkg.pr.new/@tanstack/preact-hotkeys@64@tanstack/preact-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/preact-hotkeys-devtools@64@tanstack/react-hotkeys
npm i https://pkg.pr.new/@tanstack/react-hotkeys@64@tanstack/react-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/react-hotkeys-devtools@64@tanstack/solid-hotkeys
npm i https://pkg.pr.new/@tanstack/solid-hotkeys@64@tanstack/solid-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/solid-hotkeys-devtools@64@tanstack/svelte-hotkeys
npm i https://pkg.pr.new/@tanstack/svelte-hotkeys@64@tanstack/vue-hotkeys
npm i https://pkg.pr.new/@tanstack/vue-hotkeys@64@tanstack/vue-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/vue-hotkeys-devtools@64commit:
18cf4e6No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
23f56edc-ddcf-45c9-84c6-59e6096388f4📥 Commits
Reviewing files that changed from the base of the PR and between 084836729c5f8d08c25fe96b5815d7bfd8fd7b0e and 18cf4e67aea8aa78efcee11c3dbdf8b2f6001dbf.
📒 Files selected for processing (21)
docs/reference/functions/createHotkeyHandler.mddocs/reference/functions/createMultiHotkeyHandler.mddocs/reference/functions/isSingleLetterKey.mddocs/reference/functions/normalizeKeyName.mddocs/reference/interfaces/CreateHotkeyHandlerOptions.mddocs/reference/interfaces/FormatDisplayOptions.mddocs/reference/interfaces/HotkeyCallbackContext.mddocs/reference/interfaces/ParsedHotkey.mddocs/reference/interfaces/RawHotkey.mddocs/reference/interfaces/ValidationResult.mddocs/reference/type-aliases/HeldKey.mddocs/reference/type-aliases/Hotkey.mddocs/reference/type-aliases/HotkeyCallback.mddocs/reference/type-aliases/Key.mddocs/reference/type-aliases/PunctuationKey.mddocs/reference/type-aliases/RegisterableHotkey.mddocs/reference/variables/ALL_KEYS.mddocs/reference/variables/KEY_DISPLAY_SYMBOLS.mddocs/reference/variables/MAC_MODIFIER_SYMBOLS.mddocs/reference/variables/PUNCTUATION_CODE_MAP.mddocs/reference/variables/STANDARD_MODIFIER_LABELS.md
✅ Files skipped from review due to trivial changes (21)
- docs/reference/variables/STANDARD_MODIFIER_LABELS.md
- docs/reference/functions/normalizeKeyName.md
- docs/reference/variables/MAC_MODIFIER_SYMBOLS.md
- docs/reference/functions/createHotkeyHandler.md
- docs/reference/functions/isSingleLetterKey.md
- docs/reference/type-aliases/Key.md
- docs/reference/variables/KEY_DISPLAY_SYMBOLS.md
- docs/reference/functions/createMultiHotkeyHandler.md
- docs/reference/type-aliases/Hotkey.md
- docs/reference/interfaces/CreateHotkeyHandlerOptions.md
- docs/reference/type-aliases/HotkeyCallback.md
- docs/reference/type-aliases/RegisterableHotkey.md
- docs/reference/interfaces/RawHotkey.md
- docs/reference/interfaces/FormatDisplayOptions.md
- docs/reference/interfaces/ValidationResult.md
- docs/reference/interfaces/ParsedHotkey.md
- docs/reference/variables/PUNCTUATION_CODE_MAP.md
- docs/reference/type-aliases/PunctuationKey.md
- docs/reference/type-aliases/HeldKey.md
- docs/reference/variables/ALL_KEYS.md
- docs/reference/interfaces/HotkeyCallbackContext.md
📝 Walkthrough
Walkthrough#
Adds macOS Alt/Option punctuation handling: introduces PUNCTUATION_CODE_MAP mapping KeyboardEvent.code → punctuation characters, updates matchesKeyboardEvent to fallback to code for certain punctuation keys (gated on event.code), expands punctuation key set to include
;, and updates tests and docs.Changes#
Cohort / File(s) Summary Changelog Entry
\.changeset/fix-alt-punctuation-macos.mdAdds patch release notes describing macOS Alt/Option punctuation fix and event.code fallback for punctuation keys. Documentation Reference Updates
docs/reference/functions/createHotkeyHandler.md,docs/reference/functions/createMultiHotkeyHandler.md,docs/reference/functions/isSingleLetterKey.md,docs/reference/functions/normalizeKeyName.md,docs/reference/functions/matchesKeyboardEvent.md,docs/reference/interfaces/CreateHotkeyHandlerOptions.md,docs/reference/variables/ALL_KEYS.md,docs/reference/variables/KEY_DISPLAY_SYMBOLS.md,docs/reference/variables/MAC_MODIFIER_SYMBOLS.md,docs/reference/variables/STANDARD_MODIFIER_LABELS.md,docs/reference/interfaces/*.md,docs/reference/type-aliases/*.mdAdjusted "Defined in" line references across docs; expanded matchesKeyboardEventdocs to mention punctuation fallback and dead-key/platform notes; no API changes.New Punctuation Map Documentation
docs/reference/index.md,docs/reference/variables/PUNCTUATION_CODE_MAP.mdAdds documentation for new PUNCTUATION_CODE_MAPconstant describing code→character mappings used as fallback for punctuation keys.Punctuation Constants
packages/hotkeys/src/constants.tsAdds exported PUNCTUATION_CODE_MAP: Record<string,string>mapping punctuationKeyboardEvent.codeto characters and includes';'inPUNCTUATION_KEYS.Hotkey Matching Logic
packages/hotkeys/src/match.tsImports PUNCTUATION_CODE_MAPand extendsmatchesKeyboardEventfallback to useevent.code→mapped punctuation when appropriate (gated on presence ofevent.code).Hotkey Type Update
packages/hotkeys/src/hotkey.tsAdds ';'to the exportedPunctuationKeytype.Test Coverage
packages/hotkeys/tests/match.test.tsAdds tests for punctuation fallback (dead-key and Option+punctuation scenarios), multi-modifier cases, negative cases, and cross-platform code-fallback behavior. Sequence Diagram(s)#
sequenceDiagram participant Browser as Browser (KeyboardEvent) participant Matcher as matchesKeyboardEvent participant Constants as PUNCTUATION_CODE_MAP / PUNCTUATION_KEYS Browser->>Matcher: emit KeyboardEvent (key, code, modifiers) alt event.key matches hotkey Matcher-->>Browser: match true (use event.key) else event.key doesn't match alt event.code present and is punctuation Matcher->>Constants: lookup code → char Constants-->>Matcher: mapped char alt mapped char equals hotkey key Matcher-->>Browser: match true (fallback to code) else Matcher-->>Browser: match false end else Matcher-->>Browser: match false end endEstimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem#
🐰 Option nudges keys into disguise, I mapped their codes with curious eyes. PUNCTUATION_CODE_MAP hops in neat, Semicolon joins the shortcut beat, Alt punctuation now springs to life — a prize! ✨
🚥 Pre-merge checks | ✅ 3
✏️ Tip: You can configure your own custom pre-merge checks in the settings.✅ Passed checks (3 passed)
Check name Status Explanation Title check ✅ Passed The title directly and clearly summarizes the main change: adding event.code fallback support for Alt+punctuation keys on macOS, which is the core fix across all modified files. Description check ✅ Passed The description comprehensively covers the change motivation, implementation details, test coverage, and includes a changeset entry, though the checklist items are unchecked. The core required information is present and well-organized. Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. ✨ Finishing Touches
🧪 Generate unit tests (beta)
- Create PR with unit tests
📝 Coding Plan
- Generate coding plan for human review comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands and usage tips.Actionable comments posted: 2
🧹 Nitpick comments (1)
.changeset/fix-alt-punctuation-macos.md (1)
7-7: Minor grammar fix in release note wording.Use “an
event.codefallback” for readability.✏️ Suggested edit
-On macOS, the Option (Alt) key acts as a character composer for punctuation keys (e.g., Option+- produces an en-dash '–'), causing `event.key` to differ from the expected character. Added a `event.code` fallback for punctuation keys (Minus, Equal, Slash, BracketLeft, BracketRight, Backslash, Comma, Period, Backquote, Semicolon), matching the existing fallback pattern for letter and digit keys. +On macOS, the Option (Alt) key acts as a character composer for punctuation keys (e.g., Option+- produces an en-dash '–'), causing `event.key` to differ from the expected character. Added an `event.code` fallback for punctuation keys (Minus, Equal, Slash, BracketLeft, BracketRight, Backslash, Comma, Period, Backquote, Semicolon), matching the existing fallback pattern for letter and digit keys.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.changeset/fix-alt-punctuation-macos.md at line 7, Update the release note sentence to use "an `event.code` fallback" instead of "a `event.code` fallback" for correct grammar; locate the sentence mentioning the new fallback for punctuation keys (Minus, Equal, Slash, BracketLeft, BracketRight, Backslash, Comma, Period, Backquote, Semicolon) and replace "a `event.code` fallback" with "an `event.code` fallback".🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@packages/hotkeys/src/constants.ts`: - Around line 312-323: The fallback map PUNCTUATION_CODE_MAP contains a Semicolon: ';' entry that is inconsistent with the canonical set PUNCTUATION_KEYS and the PunctuationKey type; pick one consistent fix: either remove the Semicolon entry from PUNCTUATION_CODE_MAP, or add ';' (and the corresponding "Semicolon" key) to PUNCTUATION_KEYS and update the PunctuationKey type so the canonical key set and the fallback map match; update only the symbols PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, and PunctuationKey accordingly to keep them in sync. In `@packages/hotkeys/tests/match.test.ts`: - Around line 657-679: Add the missing Semicolon entry to the punctuation test matrix so the suite title matches coverage: update the cases array in packages/hotkeys/tests/match.test.ts (the test using createKeyboardEvent and matchesKeyboardEvent) to include the Semicolon mapping (composed character produced with Alt/Option + ; on relevant layouts and the code 'Semicolon' with expectedKey ';'), then ensure the loop still asserts matchesKeyboardEvent(event, `Alt+;`) is true. --- Nitpick comments: In @.changeset/fix-alt-punctuation-macos.md: - Line 7: Update the release note sentence to use "an `event.code` fallback" instead of "a `event.code` fallback" for correct grammar; locate the sentence mentioning the new fallback for punctuation keys (Minus, Equal, Slash, BracketLeft, BracketRight, Backslash, Comma, Period, Backquote, Semicolon) and replace "a `event.code` fallback" with "an `event.code` fallback".
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
982e4e76-72f1-4915-89b4-d56e2986b8f3📥 Commits
Reviewing files that changed from the base of the PR and between 75ce7c8cf7e2ef82b1d97cc5238d5c01882d0ec9 and bb786bccc4810bca4ba302f4b33c36945acb68a1.
📒 Files selected for processing (16)
.changeset/fix-alt-punctuation-macos.mddocs/reference/functions/createHotkeyHandler.mddocs/reference/functions/createMultiHotkeyHandler.mddocs/reference/functions/isSingleLetterKey.mddocs/reference/functions/matchesKeyboardEvent.mddocs/reference/functions/normalizeKeyName.mddocs/reference/index.mddocs/reference/interfaces/CreateHotkeyHandlerOptions.mddocs/reference/variables/ALL_KEYS.mddocs/reference/variables/KEY_DISPLAY_SYMBOLS.mddocs/reference/variables/MAC_MODIFIER_SYMBOLS.mddocs/reference/variables/PUNCTUATION_CODE_MAP.mddocs/reference/variables/STANDARD_MODIFIER_LABELS.mdpackages/hotkeys/src/constants.tspackages/hotkeys/src/match.tspackages/hotkeys/tests/match.test.ts
packages/hotkeys/src/constants.ts lines 313 to 324Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -302,0 +303,21 @@303+/**304+ * Maps `KeyboardEvent.code` values for punctuation keys to their canonical characters.305+ *306+ * On macOS, holding the Option (Alt) key transforms punctuation keys into special characters307+ * (e.g., Option+Minus → en-dash '–'), causing `event.key` to differ from the expected character.308+ * However, `event.code` still reports the physical key (e.g., 'Minus'). This map enables309+ * falling back to `event.code` for punctuation keys, similar to the existing `Key*`/`Digit*`310+ * fallbacks for letters and digits.311+ */312+export const PUNCTUATION_CODE_MAP: Record<string, string> = {313+ Minus: '-',314+ Equal: '=',315+ Slash: '/',316+ BracketLeft: '[',317+ BracketRight: ']',318+ Backslash: '\\',319+ Comma: ',',320+ Period: '.',321+ Backquote: '`',322+ Semicolon: ';',323+}⚠️ Potential issue | 🟡 Minor
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash set -euo pipefail # Verify whether semicolon is part of canonical punctuation types/sets and test coverage. rg -n -C2 'type PunctuationKey|PUNCTUATION_KEYS|PUNCTUATION_CODE_MAP|Semicolon|Alt\+;|;\x27' \ packages/hotkeys/src \ packages/hotkeys/testsRepository: TanStack/hotkeys
Length of output: 2554
🏁 Script executed:
cat -n packages/hotkeys/src/hotkey.ts | sed -n '117,135p'Repository: TanStack/hotkeys
Length of output: 653
🏁 Script executed:
rg -n 'Semicolon|";"' packages/hotkeys/tests --type ts --type jsRepository: TanStack/hotkeys
Length of output: 42
🏁 Script executed:
sed -n '291,323p' packages/hotkeys/src/constants.tsRepository: TanStack/hotkeys
Length of output: 928
Semicolon entry in fallback map is inconsistent with canonical punctuation key set.
The
PUNCTUATION_CODE_MAPincludesSemicolon: ';', but;is not part ofPUNCTUATION_KEYS(lines 291-301) or thePunctuationKeytype. Either add;to the canonical punctuation keys/types or remove theSemicolonentry from the map.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/hotkeys/src/constants.ts` around lines 312 - 323, The fallback map PUNCTUATION_CODE_MAP contains a Semicolon: ';' entry that is inconsistent with the canonical set PUNCTUATION_KEYS and the PunctuationKey type; pick one consistent fix: either remove the Semicolon entry from PUNCTUATION_CODE_MAP, or add ';' (and the corresponding "Semicolon" key) to PUNCTUATION_KEYS and update the PunctuationKey type so the canonical key set and the fallback map match; update only the symbols PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, and PunctuationKey accordingly to keep them in sync.✅ Addressed in commit 0848367
packages/hotkeys/tests/match.test.ts lines 657 to 680Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -639,0 +659,21 @@659+ ['–', 'Minus', '-'], // Option+- → en-dash660+ ['≠', 'Equal', '='], // Option+= → ≠661+ ['÷', 'Slash', '/'], // Option+/ → ÷662+ ['\u201c', 'BracketLeft', '['], // Option+[ → "663+ ['\u2018', 'BracketRight', ']'], // Option+] → '664+ ['«', 'Backslash', '\\'], // Option+\ → «665+ ['≤', 'Comma', ','], // Option+, → ≤666+ ['≥', 'Period', '.'], // Option+. → ≥667+ ['`', 'Backquote', '`'], // Option+` → ` (dead key on some layouts)668+ ]669+670+ for (const [composedChar, code, expectedKey] of cases) {671+ const event = createKeyboardEvent(composedChar, {672+ altKey: true,673+ code,674+ })675+ expect(676+ matchesKeyboardEvent(event, `Alt+${expectedKey}` as Hotkey),677+ ).toBe(true)678+ }679+ })⚠️ Potential issue | 🟡 Minor
Include
Semicolonin the “all punctuation keys” matrix.The case list is close, but it skips
Semicolon, so the suite title currently overstates coverage.✅ Suggested test addition
const cases: Array<[string, string, string]> = [ ['–', 'Minus', '-'], // Option+- → en-dash ['≠', 'Equal', '='], // Option+= → ≠ ['÷', 'Slash', '/'], // Option+/ → ÷ ['\u201c', 'BracketLeft', '['], // Option+[ → " ['\u2018', 'BracketRight', ']'], // Option+] → ' ['«', 'Backslash', '\\'], // Option+\ → « ['≤', 'Comma', ','], // Option+, → ≤ ['≥', 'Period', '.'], // Option+. → ≥ + ['…', 'Semicolon', ';'], // Option+; → … ['`', 'Backquote', '`'], // Option+` → ` (dead key on some layouts) ]📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change657+ it('should fallback to event.code for all punctuation keys with Alt', () => {658+ const cases: Array<[string, string, string]> = [659+ ['–', 'Minus', '-'], // Option+- → en-dash660+ ['≠', 'Equal', '='], // Option+= → ≠661+ ['÷', 'Slash', '/'], // Option+/ → ÷662+ ['\u201c', 'BracketLeft', '['], // Option+[ → "663+ ['\u2018', 'BracketRight', ']'], // Option+] → '664+ ['«', 'Backslash', '\\'], // Option+\ → «665+ ['≤', 'Comma', ','], // Option+, → ≤666+ ['≥', 'Period', '.'], // Option+. → ≥667+ ['…', 'Semicolon', ';'], // Option+; → …668+ ['`', 'Backquote', '`'], // Option+` → ` (dead key on some layouts)669+ ]670+671+ for (const [composedChar, code, expectedKey] of cases) {672+ const event = createKeyboardEvent(composedChar, {673+ altKey: true,674+ code,675+ })676+ expect(677+ matchesKeyboardEvent(event, `Alt+${expectedKey}` as Hotkey),678+ ).toBe(true)679+ }680+ })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/hotkeys/tests/match.test.ts` around lines 657 - 679, Add the missing Semicolon entry to the punctuation test matrix so the suite title matches coverage: update the cases array in packages/hotkeys/tests/match.test.ts (the test using createKeyboardEvent and matchesKeyboardEvent) to include the Semicolon mapping (composed character produced with Alt/Option + ; on relevant layouts and the code 'Semicolon' with expectedKey ';'), then ensure the loop still asserts matchesKeyboardEvent(event, `Alt+;`) is true.✅ Addressed in commit 0848367
KevinVandy approved these changes
KevinVandy merged thisac2248c
KevinVandy closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: bb786bccc4810bca4ba302f4b33c36945acb68a1
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR