Skip to content
Source
Sign inSign up
TanStack/hotkeys

fix: add event.code fallback for Alt+punctuation keys on macOS #64

Mergeddanyi1212(GitHub profile, opens in a new tab)merged 5 commits intomain fromrefs/pull/64/headmerged by KevinVandy

Summary

  • 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. This prevented Alt+punctuation hotkeys like Alt+-, Alt+/, Alt+[, etc. from matching.
  • Added a PUNCTUATION_CODE_MAP in constants.ts that maps event.code values (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+[ when event.key is Dead).

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.code edge case
  • Added test for dead key + punctuation code fallback
  • Updated existing dead-key test to expect correct behavior
  • pnpm test passes (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. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 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
      NameType
      @tanstack/hotkeysPatch
      @tanstack/angular-hotkeysPatch
      @tanstack/hotkeys-devtoolsPatch
      @tanstack/preact-hotkeysPatch
      @tanstack/react-hotkeysPatch
      @tanstack/solid-hotkeysPatch
      @tanstack/svelte-hotkeysPatch
      @tanstack/vue-hotkeysPatch
      @tanstack/preact-hotkeys-devtoolsPatch
      @tanstack/react-hotkeys-devtoolsPatch
      @tanstack/solid-hotkeys-devtoolsPatch
      @tanstack/vue-hotkeys-devtoolsPatch

      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

    2. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented

      Open in StackBlitz

      @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@64

      commit: 18cf4e6

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      No 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.md
      • docs/reference/functions/createMultiHotkeyHandler.md
      • docs/reference/functions/isSingleLetterKey.md
      • docs/reference/functions/normalizeKeyName.md
      • docs/reference/interfaces/CreateHotkeyHandlerOptions.md
      • docs/reference/interfaces/FormatDisplayOptions.md
      • docs/reference/interfaces/HotkeyCallbackContext.md
      • docs/reference/interfaces/ParsedHotkey.md
      • docs/reference/interfaces/RawHotkey.md
      • docs/reference/interfaces/ValidationResult.md
      • docs/reference/type-aliases/HeldKey.md
      • docs/reference/type-aliases/Hotkey.md
      • docs/reference/type-aliases/HotkeyCallback.md
      • docs/reference/type-aliases/Key.md
      • docs/reference/type-aliases/PunctuationKey.md
      • docs/reference/type-aliases/RegisterableHotkey.md
      • docs/reference/variables/ALL_KEYS.md
      • docs/reference/variables/KEY_DISPLAY_SYMBOLS.md
      • docs/reference/variables/MAC_MODIFIER_SYMBOLS.md
      • docs/reference/variables/PUNCTUATION_CODE_MAP.md
      • docs/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.md
      Adds 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/*.md
      Adjusted "Defined in" line references across docs; expanded matchesKeyboardEvent docs 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.md
      Adds documentation for new PUNCTUATION_CODE_MAP constant describing code→character mappings used as fallback for punctuation keys.
      Punctuation Constants
      packages/hotkeys/src/constants.ts
      Adds exported PUNCTUATION_CODE_MAP: Record<string,string> mapping punctuation KeyboardEvent.code to characters and includes ';' in PUNCTUATION_KEYS.
      Hotkey Matching Logic
      packages/hotkeys/src/match.ts
      Imports PUNCTUATION_CODE_MAP and extends matchesKeyboardEvent fallback to use event.code→mapped punctuation when appropriate (gated on presence of event.code).
      Hotkey Type Update
      packages/hotkeys/src/hotkey.ts
      Adds ';' to the exported PunctuationKey type.
      Test Coverage
      packages/hotkeys/tests/match.test.ts
      Adds 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
          end

      Estimated 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
      ✅ Passed checks (3 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe 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✅ PassedThe 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✅ PassedDocstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ 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.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    4. 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.code fallback” 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.md
      • docs/reference/functions/createHotkeyHandler.md
      • docs/reference/functions/createMultiHotkeyHandler.md
      • docs/reference/functions/isSingleLetterKey.md
      • docs/reference/functions/matchesKeyboardEvent.md
      • docs/reference/functions/normalizeKeyName.md
      • docs/reference/index.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/PUNCTUATION_CODE_MAP.md
      • docs/reference/variables/STANDARD_MODIFIER_LABELS.md
      • packages/hotkeys/src/constants.ts
      • packages/hotkeys/src/match.ts
      • packages/hotkeys/tests/match.test.ts
    5. KevinVandy approved these changes

    6. KevinVandy merged thisac2248c

    7. KevinVandy closed this

Sign in to join this conversation.