github/hotkey

Support for KeyboardEvent code values

Closed this issue · 1 comments

In some situations, we may want to map shortcuts to physical keys instead of characters that change based on the keyboard layout.

This may be useful for websites that have an international audience, with the keyboard layout being hard to guess.

Hey @ksesong thanks for the issue

As mentioned in the MDN docs that you link:

For example, the code returned is "KeyQ" for the Q key on a QWERTY layout keyboard, but the same code value also represents the ' key on Dvorak keyboards and the A key on AZERTY keyboards. That makes it impossible to use the value of code to determine what the name of the key is to users if they're not using an anticipated keyboard layout.

To determine what character corresponds with the key event, use the KeyboardEvent.key property instead.

In the case of shortcut combinations, they rely on discoverability through help documentation (on GitHub we use the ? key to bring up a list of all shortcuts) - in turn these help docs rely on rendering the printable character, and so in turn we must rely on what would be the printed character, not the keycode. Using event.key actually lets us handle a variety of keyboard layouts, as mentioned in the quoted docs above.

For internationalization of keyboard shortcuts, you might instead consider internationalizing your keyboard shortcuts! For example if you expect users to invoke alt+e, adding alt+€ will anticipate shortcuts coming from French keyboard layouts. Unfortunately there's no pre-determined list of these that can be provided, and this is a very complex and nuanced problem.

I'm going to close this because I dont think swapping to event.code will not solve the problem, nor will providing it as an option.