KeyboardEvent key Property

Example

What key was pressed when a key event occured:

let key = event.key;
Try it Yourself »

More examples below.


Description

The key property returns the key that was pressed when the event occured.

The key property is read-only.

The return value can be:

  • A single character ("a", "W", "4", "+", "$")
  • Multiple characters ("F1", "Enter", "HOME", "CAPS LOCK")


Syntax

event.key

Technical Details

Return Value: A String
The key that was pressed:

A single character ("A", "a", "4", "+", "$")
Multiple characters ("F1", "Enter", "HOME", "CAPS LOCK")
DOM Version: DOM Level 3 Keyboard Events.

More Examples

Example

Write some text if the user presses the "A" key:

let key = event.key;

if (key == "a" || key == "A") {
  let text = "You pressed the 'A' key!";
}
Try it Yourself »

Browser Support

event.key is a DOM Level 3 (2004) feature.

It is fully supported in all modern browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 11


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.