Favorites

DOM Element: keydown event

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element: keydown event
DOM Element EventThis event is supported by all modern browsers.
Updated 28th November 2026

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

document.addEventListener('keydown', e => {
  if (e.key === 'Escape') console.log('Escape pressed');
  else if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
    e.preventDefault();
    console.log('Open search shortcut');
  } else console.log('key:', e.key, 'repeat:', e.repeat);
});
console.log('Click inside this frame, then press keys');

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.

This event is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee