Bug 174782 - KeyboardEvent#key reports lowercase letter with Shift+Cmd
Summary: KeyboardEvent#key reports lowercase letter with Shift+Cmd
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: Safari 10
Hardware: Mac macOS 10.12.4
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-07-24 03:26 PDT by Keith Cirkel
Modified: 2020-04-05 22:32 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Keith Cirkel 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
```
Comment 1 Keith Cirkel 2017-07-24 03:30:05 PDT
I've just tested this in Safari 11 TP and this bug is also present there.
Comment 2 Alexey Proskuryakov 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.
Comment 3 Keith Cirkel 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.
Comment 4 Alexey Proskuryakov 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.
Comment 5 Lucas Forschler 2019-02-06 09:18:48 PST
Mass move bugs into the DOM component.
Comment 6 Marijn Haverbeke 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.
Comment 7 Keith Cirkel 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'`
Comment 8 Marijn Haverbeke 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.