DOM KeyboardEvent.code Property
The code property of a keyboard event identifies the physical key pressed, such as KeyW, regardless of keyboard layout.
Description
The read-only code property identifies the physical key on the keyboard, independent of the layout and modifiers. The key in the "Q" position on a US keyboard is always "KeyQ", even on a French AZERTY keyboard where it types "a". That makes it ideal for game controls; for text and shortcuts users read off their keys, key is the better choice.
Because code ignores the layout, it can't tell you which character a key produces; showing "Press W to move" to a user with an AZERTY keyboard would be misleading. The Keyboard API's navigator.keyboard.getLayoutMap(), available in Chromium browsers, can map codes to the characters on the user's layout.
Modifier keys have codes for each side, such as "ShiftLeft" and "ShiftRight", which key doesn't distinguish. Virtual keyboards on phones often report an empty or generic code.
Syntax
event.codeValue
A string naming the physical key, such as "KeyA", "Digit1" or "Space".
Example
Browser Support
The following information will show you the current browser support for the KeyboardEvent.code property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





