Feature: support physical key (event.code) matching for non-Latin IME input methods #101
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:
- Switch OS input method to Korean (or any non-Latin IME)
- Register
useHotkey('A', callback) - Press the physical
Akey - 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.
Related#
- #65 (Discussion: Add support for international keyboard layouts)
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
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.
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.
Sometime soon, this library is going to get a matching overhaul based on event.code
KevinVandy closed this
Sign in to join this conversation.
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,matchesKeyboardEventhas this block:isSingleLetterKeyuses/^\p{Letter}$/u, which matches any Unicode letter — including Greek (ν,γ,δ…). So when a Greek-layout user presses the physicalNkey:event.key='ν'event.code='KeyN'isSingleLetterKey('ν')→true!event.altKey→truereturn falsefires, theevent.codefallback is never reachedThis affects any non-Latin alphabet keyboard layout where
event.keyproduces 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:
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 fireuseHotkey('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:useHotkey('N', handler)— only fires whenevent.keyproducesNuseHotkey('N', handler, { matchBy: 'code' })— fires onKeyNregardless of layoutApps 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 patchto apply the naive fix locally and accepting the trade-off. Happy to test amatchBy: 'code'implementation if a branch becomes available.