-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Add KeyboardEvent.unmodifiedKey #229
Comments
The proposed solution for this problem is Keyboard Map: (Link to original proposal: https://github.com/WICG/keyboard-map/blob/master/proposal.md) For this, you would record the |
@garykac Thanks very much! I wasn't aware of the Keyboard Map proposal. I've just given it a read, along with the original proposal/explainer, and it would indeed solve the problem for our use case — assuming that the privacy repercussions don't render the API unusable. I'm concerned that, if browsers require a permission prompt (as mentioned in Privacy Mitigations), this would be out of context, overly confusing and annoying. Our app, a music notation editor, displays the default keyboard shortcuts directly in context, in tooltips for each of the dozens of UI buttons: As it stands, I guess we'd need to show the permission prompt at the time of page load, which would be confusing because it wouldn't be directly tied to an obvious keyboard-shortcut user action. Granted, this is a theoretical argument, because the spec leaves the permission-prompt question to browser implementations. But couldn't we sidestep that problem entirely if we provided the data in the |
You can try it out for yourself since there is an implementation in Chrome (enabled since Chrome 69): MDN docs: https://developer.mozilla.org/en-US/docs/Web/API/Keyboard_API Including it automatically in the KeyboardEvent means that there wouldn't be an obvious place for the UA to ask for permission (should it choose to do that). |
A web app I work on, Soundslice, lets people add custom keyboard shortcuts. The shortcut-customization UI uses
KeyboardEvent.key
to display which key combination you've entered, and it uses the value ofKeyboardEvent.key
internally to save the user's custom shortcuts. More info on the specific product feature is here.This generally works OK, but we have a problem with modifier keys. If you try to enter
Option g
as a shortcut (i.e., theOption
key plus theg
key),KeyboardEvent.key
returns the copyright symbol "©" (on my particular keyboard locale). That's becauseKeyboardEvent.key
returns the string that would result from your keypress according to the modifier keys and keyboard locale.I understand the reasoning for this, and in fact it's the whole point of
KeyboardEvent.key
as opposed toKeyboardEvent.code
— but neitherkey
norcode
nicely handle this particular use case of custom keyboard shortcuts. A person creating a keyboard shortcut is thinking about the literal keys —Option G
in this case — not the resulting character string.At first glance, that suggests we should be using
KeyboardEvent.code
. The problem there is: we also want the keyboard layout to be honored. On an AZERTY keyboard, hitting the "A" key setsKeyboardEvent.code
toKeyQ
andKeyboardEvent.key
toa
— the latter is what we want.This essentially means there is no consistent cross-platform or cross-locale way of checking for keyboard events with the
Option
modifier set.In short, I believe this use case would be handled elegantly if
KeyboardEvent
had anunmodifiedKey
attribute. This would be the same value asKeyboardEvent.key
(i.e., taking keyboard locale into account), but without taking any modifier keys into account. This gives us the best of both worlds — the keyboard locale handling fromKeyboardEvent.key
and the physical-key-centric approach ofKeyboardEvent.code
.If there's an existing way to accomplish this, I'd of course love to hear it! Thanks for reading. :)
The text was updated successfully, but these errors were encountered: