KeyboardEvent key Attribute Sample

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

Background

The KeyboardEvent.key attribute returns the string value of the key (or keys) pressed by the user to generate a KeyboardEvent.

Special control keys that do not have a direct string representation are given key values that can be found in this chart. For example, pressing the caps lock key will result in a key value of "CapsLock".

Live Output

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


JavaScript Snippet

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