Latest web development tutorials

key event properties

Event Object Reference Event objects

Examples

Get keyboard button press the button:

var x = event.key;

x The output is:

Enter

try it"

Bottom of this article contains more examples.


Definitions and use

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

The key identifier is a string representation of a keyboard button, the return value of this property can be:

  • Single letter (e.g. "a", "W", "4", "+" or "$")
  • More letters (such as "F1", "Enter", "HOME" or "CAPS LOCK")

Tip: If you want to see if the press "ALT", "CTRL", "META" or "SHIFT" key, you can use altKey , ctrlKey , metakey or shiftKey property.


Browser Support

Figures in the table represent the first browser to support the property version number.

Attributes
key not support 9.0 23.0 not support not support


grammar

event .key

technical details

return value: String that represents the key button.

Possible values:
  • Single letter (e.g. "a", "W", "4", "+" or "$")
  • More letters (such as "F1", "Enter", "HOME" or "CAPS LOCK")
Note: Chrome, Safari and Opera browser returns undefined
DOM version: DOM Level 3 Events


Examples

More examples

Examples

When the user presses the "A" key to pop up a message:

var x = event.key;

// Press "a" or "A" key (use caps lock or shift), the pop-up message

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

try it"


Related Pages

HTML DOM Reference Manual: Key Event Properties

HTML DOM Reference: charCode Event Properties

HTML DOM Reference Manual: Which Event Properties

Event Object Reference Event objects