Favorites

DOM KeyboardEvent.code Property

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
KeyboardEvent.code
DOM KeyboardEvent PropertyThis property is supported by all modern browsers.
Updated 28th November 2026

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.code

Value

A string naming the physical key, such as "KeyA", "Digit1" or "Space".

Example

document.addEventListener('keydown', e => {
  console.log('code:', e.code, '| key:', e.key);
  if (['KeyW', 'KeyA', 'KeyS', 'KeyD'].includes(e.code)) console.log('Movement key');
});
console.log('Click inside the frame and press keys');

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.

This property is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee