I’m trying to implement a keyboard shortcut that triggers when the user presses: • Windows/Linux: Alt + Z • macOS: Option + Z
The problem is with using event.code === 'KeyZ'. It works fine on a QWERTY keyboard, but breaks on other keyboard layouts (e.g., AZERTY or Dvorak), where the 'Z' key might be in a different physical position.
I want the shortcut to work based on the character ('z' or 'Z'), not the physical key.
How can I reliably detect Alt/Option + Z across platforms and keyboard layouts using JavaScript?
What I’ve tried:
filter((event: KeyboardEvent) => event.altKey && event.code === 'KeyZ'),
Any best practices for layout-independent shortcut handling?