[w3c/uievents] Need property which gives the (base) unmodified key (#247)

I run a fairly popular website with text editors for many languages ([www.typeit.org](https://www.typeit.org/)). I am currently using the legacy API (`.charCode`, `.keyCode`), and am looking into migrating to the new one. Unfortunately, it looks like the new API is missing some functionality of the old one.

Consider this example:

Suppose I wanted to use `KeyboardEvent` (the new API, not the legacy properties) to bind Alt+Z to some symbol.
For example, if the user wants to type in Polish, I want to tell them to "**press Alt+Z to type the Polish letter ż**" (z with a dot).
What does "Alt+Z" mean to the user? It means "hold Alt and press the key which has Z printed on it".
(In this example, Alt includes AltGr, as they are considered the same by regular users.)

**The `KeyboardEvent` API, as it is currently specced, does not allow me to do this reliably.**

- Suppose the user is using the [German T2](https://en.wikipedia.org/wiki/German_keyboard_layout) keyboard layout.
- The user presses AltGr+Z according to my instructions.
- How can I detect that they pressed this key?
1. `KeyboardEvent.key` returns "Dead" (because AltGr+Z is a dead key in the German T2 layout)
2. `KeyboardEvent.code` returns "KeyY" (because German Z is physically located where the Y is on a US keyboard)

**There is no property that would tell me that the user just pressed a key which is labeled "Z" on the keyboard.**

Of course, the same problem will occur on any non-QWERTY keyboard layout with AltGr-activated characters. For now, I can still access the legacy property `KeyboardEvent.keyCode` – this returns the ASCII code of the key that would be inserted if the key were pressed without modifiers (in this case, Z). But the specification does not require browser developers to support this property. It is already deprecated according to MDN. 

For this reason, I suggest one the following changes to the spec (in order of preference):

1. Add a `KeyboardEvent.baseKey` property that would return the character that would be inserted if the current key were pressed without modifiers (with letters normalized to uppercase). There should be a special exception for AZERTY digits, i.e. pressing AltGr+2 should result in `.baseKey` == "2", even though a bare "2" keypress produces é (not 2) in AZERTY. (I believe this reflects the current browser status quo for `.keyCode`.)

2. Make `KeyboardEvent.keyCode` mandatory.

This would give Web developers an official, standard way to refer to keys by their "keycap name".



-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/uievents/issues/247

Received on Wednesday, 30 October 2019 19:33:40 UTC