Make key filters work on non-latin keyboard layouts by psd-coder · Pull Request #869 · hotwired/stimulus
Overview
Fixes key filter matching in Stimulus actions so that keyboard shortcuts defined with ASCII characters (e.g., keydown.j) work correctly when the user's active keyboard layout produces non-ASCII characters (Cyrillic, Greek, Hebrew, etc.).
Problem Statement
When users have a non-latin keyboard layout active (e.g., Russian phonetic, Greek, Hebrew), event.key contains the non-ASCII character produced by their layout rather than the Latin letter on the physical key. This caused key filters like keydown.j->controller#action to never match, making keyboard shortcuts unusable for non-latin keyboard users.
Solution Approach
- Added
normalizedKeyboardEventKey()that falls back to deriving the key fromevent.codewhen the mapped filter key is a printable ASCII character butevent.keyis not - The fallback extracts the Latin letter or digit from
event.code(e.g.,KeyJ→j,Digit3→3) so physical key position is used instead of the produced character - Fallback is skipped during IME composition (
event.isComposing) to avoid interfering with input method editors - Added tests for Cyrillic (Bulgarian phonetic), Greek, and Hebrew layouts
Breaking Changes
None.
Testing
- Three new test cases covering Cyrillic (й/KeyJ), Greek (ξ/KeyJ), and Hebrew (ח/KeyJ) keyboard layouts verifying the fallback works correctly