I have an application that relies on the deprecated keyCode property for keyboard shortcuts. I need to update my code to use a modern alternative that works consistently regardless of the user's keyboard configuration or language.
The MDN documentation suggest using either KeyboardEvent.code or KeyboardEvent.key. However, neither one of them fully substitutes keyCode in my use cases. Initially, I decided to use code as it seems more language-independent. However, I encountered issues, such as having to handle both the regular keys and the numpad keys separately (Enter, NumpadEnter, Numpad1...).
The current problem I'm facing is caused by mismatched keyboard layouts. For example, when using a QWERTY physical keyboard with an AZERTY OS layout and pressing Ctrl+A:
Pressing the "A" (physical "Q") key gives:
code = "KeyQ",key = "a"My
codecheck fails, because it expects"KeyA"
I could check key instead, but this breaks with non-latin languages where the same physical key produces different characters (Cyrillic, Greek, etc.). I'd need extensive checks to ensure all characters that previously worked are handled now, too.
My question is whether there is a better and more modern approach to handle keyboard shortcuts reliably. What are you using in your application as a replacement of keyCode?