DOM KeyboardEvent Interface
KeyboardEvent describes keyboard interaction, such as keydown and keyup events, including which key was pressed and whether modifier keys were held.
Description
The KeyboardEvent interface describes a user interaction with the keyboard. The main events are keydown, fired when a key is pressed (and repeatedly while held, with repeat set to true), and keyup, fired when it's released. The older keypress event is deprecated.
Use key for the character or name of the key, such as "a", "Enter" or "ArrowUp", and code for the physical key position, such as "KeyA". The deprecated keyCode and which numbers should no longer be used. For text entered into fields, listen for the input event instead, which also covers pasting, voice input and IMEs.
Keyboard events go to the element that has focus, or to the body if nothing is focused, and bubble up to the document, so global shortcuts are usually handled with one listener on document. Always make sure shortcuts don't interfere with typing in form fields, and don't rely on keyboard events alone for essential functions, since some users enter text with voice input or on-screen keyboards.
Inheritance
KeyboardEvent inherits properties and methods from Event.
Properties
| Name | Description |
|---|---|
KeyboardEvent.code | The code property of a keyboard event identifies the physical key pressed, such as KeyW, regardless of keyboard layout. |
KeyboardEvent.key | The key property of a keyboard event gives the value of the key pressed, such as a, Enter or ArrowUp, taking the keyboard layout and Shift into account. |
Example
Browser Support
The following information will show you the current browser support for the KeyboardEvent interface. Hover over a browser icon to see the version that first introduced support for this interface.
Desktop





Tablets & Mobile





