KeyboardEvent code Attribute Sample

Available in Chrome 48+ | View on GitHub | Browse Samples

Background

The KeyboardEvent.code attribute represents the physical key that was used to generate a KeyboardEvent.

The operating system-level keyboard layout does not affect the KeyboardEvent.code value. In other words, while the physical key Q represents the character q in a QWERTY keyboard layout and represents the character ' in a Dvorak keyboard layout, pressing the Q key will always yield a KeyboardEvent.code value of 'KeyQ'.

Live Output

The KeyboardEvent.code values for any keys you press on the keyboard will be logged here.


JavaScript Snippet

window.addEventListener('keydown', function(event) {
  if ('code' in event) {
    ChromeSamples.log('KeyboardEvent.code:', event.code);
  } else {
    ChromeSamples.setStatus('KeyboardEvent.code is not supported.');
  }
});