w3c / uievents

UI Events
https://w3c.github.io/uievents/
Other
145 stars 51 forks source link

KeyboardEvent.key value definition does not match with existing browsers when modified by AltGraph key does not produce any characters #252

Open masayuki-nakano opened 4 years ago

masayuki-nakano commented 4 years ago

https://w3c.github.io/uievents-key/#h-clipboard-read

  1. Else, if the key event has any modifier keys other than glyph modifier keys, then

With this definition, if AltGraph + f does not reproduce character, the key value becomes Unidentified.

IIRC, with the older draft, the key value should be computed without AltGraph key in this case. Therefore, any browsers behave so (on Firefox, probably due to a bug, characters without AltGraph is inputted, though).

Additionally, even if this is false, the key value should be empty string rather than Unidentified because of not a function key.

tszynalski commented 4 years ago

Actually, it was phrased that way as early as 2015. See https://www.w3.org/TR/2015/WD-uievents-20151215/#key-algorithm (notice Shift and AltGr are specifically excluded).

Returning Unidentified for nonprintable combos allows one to distinguish between AltGr combinations which produce a printable char on the current keyboard layout and those which are unbound.

To illustrate the problems with the current implementations, suppose we want to convert a straight apostrophe to a "curly" one in a text editor. To do this, we want to detect whenever the user types ' (apostrophe), regardless of what key or key combo is necessary to do so in the user's keyboard layout.

Now suppose we get a keydown event with .key == "'" (apostrophe) and AltGr pressed. What does this mean?

  1. The user pressed an AltGr combination which types ' on the current keyboard layout (e.g. Alt+G or Alt+Shift+2) or
  2. The user pressed AltGr+' (AltGr with a key whose unmodified function is to type ') and that combination is unbound.

Which is it? With the current behavior, we can't tell. But we only want to react if it's (1).

Anticipating possible objections: listening for beforeinput is not a substitute, because we only want to react to keyboard input, not other operations that might insert characters.