Favorites

DOM KeyboardEvent Interface

☕
Enjoying this site?

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

Buy us a coffee
KeyboardEvent
DOM InterfaceThis interface is supported by all modern browsers.
Updated 28th November 2026

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

NameDescription
KeyboardEvent.codeThe code property of a keyboard event identifies the physical key pressed, such as KeyW, regardless of keyboard layout.
KeyboardEvent.keyThe 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

<input id="field" placeholder="Type here and press Enter">
const field = document.getElementById('field');
field.addEventListener('keydown', e => {
  console.log(`key: ${e.key}, code: ${e.code}, ctrl: ${e.ctrlKey}`);
  if (e.key === 'Enter') console.log('Submitted:', field.value);
});
console.log('Click the field and type');

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.

This interface 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