Easy Tutorial
❮ Event Onmouseover Met Namednodemap Removenameditem ❯

key Event Property

Event Object

Example

Get the keyboard button when a key is pressed:

var x = event.key;

x outputs:

Enter

More examples are included at the bottom of this page.


Definition and Usage

The key event returns the identifier of the key pressed.

The key identifier is a string representing the keyboard button, and the return value can be:

Tip: If you want to check if the "ALT", "CTRL", "META", or "SHIFT" keys are pressed, use the altKey, ctrlKey, metaKey, or shiftKey properties.


Browser Support

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

Property Chrome IE Firefox Safari Opera
key No support 9.0 23.0 No support No support

Syntax

Technical Details

Return Value: String, representing the key button. <br> <br>Possible values: Single letter (e.g., "a", "W", "4", "+", or "$")<br> Multiple letters (e.g., "F1", "Enter", "HOME", or "CAPS LOCK") Note: Chrome, Safari, and Opera browsers return undefined
DOM Version: DOM Level 3 Events
--- ---

More Examples

Example

Display an alert message when the user presses the "A" key:

var x = event.key; // Press "a" or "A" key (using caps lock or shift), display alert message
if (x == "a" || x == "A") {
    alert("You pressed the 'A' key!");
}

Related Pages

HTML DOM Reference: key Event Property

HTML DOM Reference: charCode Event Property

HTML DOM Reference: which Event Property

Event Object

❮ Event Onmouseover Met Namednodemap Removenameditem ❯