Eejdoowad
Eejdoowad

Reputation: 1381

How to programmatically determine KeyboardEvent.key from KeyboardEvent.code and modifierKeys

My understanding is that in the future, browsers will only support two properties for determining which key was pressed, KeyboardEvent.key and KeyboardEvent.code.

In theory, KeyboardEvent.key is a function of:

Given all those dependencies are available, how do you get KeyboardEvent.key programmatically?

Upvotes: 2

Views: 141

Answers (1)

Eejdoowad
Eejdoowad

Reputation: 1381

If anyone ever wanders across here... the answer is you can't really. event.key gets you the character that is produced, while event.code gets you the physical location of the key. You need to know the keyboard layout (e.g. US vs Korean vs Brazilian vs something else) to map from physical key position to the character produced. Refer to the keyboard events spec for more info.

Update:

Chrome 66 introduced the Keyboard API which lets you find the string associated with a given key code. It does not appear to support modifier keys.

Upvotes: 1

Related Questions