THE WORLD'S LARGEST WEB DEVELOPER SITE

KeyboardEvent key Property

❮ Event Object

Example

Get the keyboard button that was pressed when a key event occured:

var x = event.key;

The result of x could be:

Enter
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

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

Key identifiers are strings that identify keyboard buttons. The return value of this property can be a string of:

  • A single character (like "a", "W", "4", "+" or "$")
  • A multicharacter (like "F1", "Enter", "HOME" or "CAPS LOCK")

Note: This property is read-only.

Tip: If you want to find out whether the "ALT", "CTRL", "META" or "SHIFT" key was pressed when a key event occured, use the altKey, ctrlKey, metaKey or shiftKey property.


Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property
key 51.0 9.0 23.0 Not supported 38.0


Syntax

event.key

Technical Details

Return Value: A String, representing the pressed keyboard button.

Possible values:
  • A single character (like "a", "W", "4", "+" or "$")
  • A multicharacter (like "F1", "Enter", "HOME" or "CAPS LOCK")
Note: Returns undefined in Safari
DOM Version: DOM Level 3 Events

More Examples

Example

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

var x = event.key;

// If the pressed keyboard button is "a" or "A" (using caps lock or shift), alert some text.

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

Related Pages

HTML DOM reference: KeyboardEvent keyCode Property

HTML DOM reference: KeyboardEvent which Property

HTML DOM reference: KeyboardEvent charCode Property


❮ Event Object