Bug 174782

Summary: KeyboardEvent#key reports lowercase letter with Shift+Cmd
Product: WebKit Reporter: Keith Cirkel <webkit>
Component: DOMAssignee: Nobody <webkit-unassigned>
Status: NEW    
Severity: Normal CC: ap, marijnh, me, wenson_hsieh
Priority: P2    
Version: Safari 10   
Hardware: Mac   
OS: macOS 10.12.4   

Keith Cirkel
Reported 2017-07-24 03:26:54 PDT
KeyboardEvent#key is supposed to report the key character that was pressed. The spec - specifically 5.3.2 "modifier keys" (https://www.w3.org/TR/uievents/#keys-modifiers) shows that if the `shiftKey` is true, then the `event.key` value should be in its "shifted state" - this includes when pressed in combination with other keys - for example `metaKey` (in Example 24 (https://www.w3.org/TR/uievents/#example-aa6d4cea) they use `ctrlKey` and `shiftKey`). Safari misreports this, contrary to other browsers. An example: ``` // Pressing Shift+P results in: event.shiftKey && event.key === 'P' // Pressing Ctrl+Shift+P results in: event.ctrlKey && event.shiftKey && event.key === 'P' // Pressing Cmd+Shift+P results in: event.metaKey && event.shiftKey && event.key === 'p' // lowercase, incorrect ``` To compare Firefox, Chrome, report the following: ``` // Pressing Shift+P results in: event.shiftKey && event.key === 'P' // Pressing Ctrl+Shift+P results in: event.ctrlKey && event.shiftKey && event.key === 'P' // Pressing Cmd+Shift+P results in: event.metaKey && event.shiftKey && event.key === 'P' // uppercase, correct ```
Attachments
Keith Cirkel
Comment 1 2017-07-24 03:30:05 PDT
I've just tested this in Safari 11 TP and this bug is also present there.
Alexey Proskuryakov
Comment 2 2017-07-27 13:49:54 PDT
I don't think that this is a bug. If you look in the Keyboard Viewer <https://support.apple.com/kb/PH25242?locale=en_US>, you can see that the Cmd+Shift plane is lower case on the Mac, so this is what WebKit exposes.
Keith Cirkel
Comment 3 2017-07-28 01:13:56 PDT
It's a bug in as much as every other browser on Mac represents the keys in upper case, and the spec for event.key has examples showing the ctrl/cmd+shift+letter keys use their uppercase variants.
Alexey Proskuryakov
Comment 4 2017-07-28 14:45:42 PDT
We should get the spec examples fixed. Diverging from native behavior would be a bad idea, both because it would be confusing, but also because it actually matters and makes writing reliable software easier.
Lucas Forschler
Comment 5 2019-02-06 09:18:48 PST
Mass move bugs into the DOM component.
Marijn Haverbeke
Comment 6 2020-03-13 03:57:39 PDT
I want to argue that conforming to other platforms and browsers is more important than conforming to the native behavior. Safari runs web apps, which target the web in general, and having to special-case key handling per platform is antithetical to that. Specifically, I maintain a library that allows client code to specify key maps, and uses KeyboardEvent.key in its key dispatching logic. Having such bindings work differently across browsers and platforms is obviously causing problems for my users.
Keith Cirkel
Comment 7 2020-04-02 09:27:08 PDT
It looks like this was fixed back in Chrome 62, via a related bug: https://crbug.com/747358. As far as my testing today goes, all browsers report consistently that `event.metaKey && event.shiftKey && event.key === 'p'`
Marijn Haverbeke
Comment 8 2020-04-05 22:32:27 PDT
It's not really fixed, as per the original bug description—it rather seems like other browsers have regressed to also behave in the incorrect way. From some of the related bugs, it seems that macOS makes it very hard to get the correct character in this case, so maybe that's how this happened. But I don't think this issue can be considered solved.
Note You need to log in before you can comment on or make changes to this bug.