Key event properties

key incident attribute The event object

Get the keyboard button when you press the key:

var x = event.key;

x The output is:

Enter

Try it out . . .

More examples are included at the bottom of this article.


Define and use

The key event returns the identifier of the key when the key is pressed.

A key identifier is a string that represents a keyboard button, and the return value of the property can be:

  • Individual letters (e.g. "a", "W", "4", "plus" or "$")
  • Multiple letters (e.g. "F1," "Enter," "HOME" or "CAPS LOCK")

Tip: If you want to see if you press the "ALT", "CTRL", "META" or "SHIFT" keys, you can use the altKey, ctrlKey, metaKey, or shiftKey properties.


Browser support

The numbers in the table support the version number of the first browser for this property.

Property
key Not supported 9.0 23.0 Not supported Not supported


Grammar

event .key

Technical details

Return value: A string that represents a button button.

Possible values:
  • Individual letters (e.g. "a", "W", "4", "plus" or "$")
  • Multiple letters (e.g. "F1," "Enter," "HOME" or "CAPS LOCK")
Note: Chrome, Safari and Opera return to undefined
DOM version: DOM Level 3 Events


key incident attribute

More instances

A prompt pops up when the user presses the "A" key:

var x = event.key;

Press the "a" or "A" key (using caps lock or shift) to eject the prompt

if (x == "a" || x == "A" ) {
alert ("You pressed the 'A' key!"). );
}

Try it out . . .


Related pages

HTML DOM Reference Manual: Key Event Properties

HTML DOM Reference Manual: charCode Event Properties

HTML DOM Reference Manual: Which Event Properties

key incident attribute The event object