Skip to content
Source
Sign inSign up
TanStack/hotkeys

Feature: support physical key (event.code) matching for non-Latin IME input methods #101

ClosedHwangSB(GitHub profile, opens in a new tab) opened this issue 3 comments

TanStack Hotkeys version

0.7.0

Framework/Library version

React 19

Describe the bug and the steps to reproduce it

When a non-Latin input method is active (e.g., Korean, Japanese, Chinese, Russian IME), single-key hotkeys do not fire because event.key produces a non-Latin character instead of the expected ASCII letter.

For example, with Korean input active, pressing the physical A key produces:

  • event.key = 'ㅁ'
  • event.code = 'KeyA'

So a hotkey registered as A will not match.

The current matching logic in matchesKeyboardEvent (match.ts) trusts the keyboard layout for non-ASCII letters without Alt:

if (
  isSingleLetterKey(eventKey) &&
  (/^[A-Za-z]$/.test(eventKey) || !event.altKey)
) {
  return false  // trusts layout, never reaches event.code fallback
}

This correctly supports alternative Latin layouts (Dvorak, Colemak, AZERTY). However, for IME-based input methods where the physical layout is QWERTY but the output characters are non-Latin, the event.code fallback is never reached, and the hotkey silently fails.

Steps to reproduce:

  1. Switch OS input method to Korean (or any non-Latin IME)
  2. Register useHotkey('A', callback)
  3. Press the physical A key
  4. Callback never fires

This affects all non-Latin IME users globally: Korean, Japanese, Chinese, Russian, Arabic, Thai, and more. Users frequently switch between their native input method and English throughout their workflow and expect single-key shortcuts to work without manually switching input language first.

Proposed solution

An explicit opt-in option for event.code based (physical key) matching:

// Per-registration
useHotkey('A', callback, { matchBy: 'code' })

// Or via useHotkeys
useHotkeys(
  [
    { hotkey: 'A', callback: handleA },
    { hotkey: 'S', callback: handleS },
  ],
  { matchBy: 'code' }
)

When matchBy: 'code' is set, matchesKeyboardEvent would compare the hotkey against event.code (e.g., 'A' matches KeyA) instead of event.key. The default remains 'key' to preserve current behavior.

This avoids the conflict with automatic fallback approaches (see #53) because the consumer explicitly opts in per registration. Applications that need layout-aware matching (Dvorak, Colemak) keep the default 'key' behavior, while applications that need physical key matching can opt in.

  • #65 (Discussion: Add support for international keyboard layouts)

No response

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

  • I intend to try to help solve this bug with my own PR

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  1. Running into the same issue with a Greek keyboard layout — adding some detail that may help move this forward.

    The exact code path

    In packages/hotkeys/src/match.ts, matchesKeyboardEvent has this block:

    if (
      isSingleLetterKey(eventKey) &&
      (/^[A-Za-z]$/.test(eventKey) || !event.altKey)
    ) {
      return false
    }

    isSingleLetterKey uses /^\p{Letter}$/u, which matches any Unicode letter — including Greek (ν, γ, δ…). So when a Greek-layout user presses the physical N key:

    • event.key = 'ν'
    • event.code = 'KeyN'
    • isSingleLetterKey('ν') → true
    • !event.altKey → true
    • → return false fires, the event.code fallback is never reached

    This affects any non-Latin alphabet keyboard layout where event.key produces a Unicode letter: Greek, Cyrillic, Hebrew, Arabic, Thai, etc.

    Why a simple fix to the default behaviour isn't the right call

    I initially patched the early-return condition to only check ASCII letters:

    // proposed naive fix
    if (/^[A-Za-z]$/.test(eventKey) && !event.altKey) return false

    But I understand why that would be controversial — it changes the intentional design for Cyrillic users who want layout-aware shortcuts (pressing Cyrillic н should not fire useHotkey('N')).

    The matchBy: 'code' approach is the right solution

    The opt-in matchBy: 'code' option proposed in this issue is exactly what's needed. It lets each app decide:

    • Layout-aware (default): useHotkey('N', handler) — only fires when event.key produces N
    • Physical-position: useHotkey('N', handler, { matchBy: 'code' }) — fires on KeyN regardless of layout

    Apps targeting multilingual users (where keyboard layout may vary) can opt in per-registration without affecting anyone who relies on the current behaviour.

    Current workaround

    Until this lands, I'm using bun patch to apply the naive fix locally and accepting the trade-off. Happy to test a matchBy: 'code' implementation if a branch becomes available.

  2. I can reproduce this with the Georgian keyboard layout .

    Package: @tanstack/react-hotkeys 0.10.0

    Code: useHotkey("Mod+K", openPanel);

    With the English layout, Ctrl+K works. With the Georgian layout, it does not fire.

    KeyboardEvent values when pressing the physical K key:

    event.key: "კ" event.code: "KeyK" event.ctrlKey: true

    Matching through event.code, or an option such as matchBy: "code", would solve this for Georgian users as well.

  3. Sometime soon, this library is going to get a matching overhaul based on event.code

  4. KevinVandy closed this

Sign in to join this conversation.