DOM Element: keydown event
The keydown event fires when a key is pressed while an element (or something inside it) has focus, and repeats while the key is held.
Description
The keydown event fires when a key is pressed, on the element that has focus, and bubbles up to its ancestors and the document. It fires repeatedly while the key is held, with event.repeat set to true. The keyup event fires when the key is released.
Use event.key to find out which key it was. Calling preventDefault() stops the key's default action, such as typing a character or scrolling. To react to text being entered, the input event is more reliable.
keydown fires on the focused element, so to listen for shortcuts anywhere on the page, add the listener to document. When the user is typing in a field, check event.target so your shortcuts don't trigger while they type, and avoid overriding browser and screen reader shortcuts.
Syntax
element.addEventListener('keydown', handler)Example
Browser Support
The following information will show you the current browser support for the Element: keydown event event. Hover over a browser icon to see the version that first introduced support for this event.
Desktop





Tablets & Mobile





