GO-V.ORG
As seen in

Element: keydown event

UI Events

UI Events

The **`keydown`** event is fired when a key is pressed.

Unlike the deprecated keypress event, the `keydown` event is fired for all keys, regardless of whether they produce a character value.

The `keydown` and [`keyup`](/docs/Web/API/Element/keyup_event) events provide a code indicating which key is pressed, while `keypress` indicates which character was entered. For example, a lowercase "a" will be reported as 65 by `keydown` and `keyup`, but as 97 by `keypress`. An uppercase "A" is reported as 65 by all events.

The event target of a key event is the currently focused element which is processing the keyboard activity. This includes: input, textarea, anything that is [`contentEditable`](/docs/Web/HTML/Reference/Global_attributes/contenteditable), and anything else that can be interacted with the keyboard, such as a, button, and summary. If no suitable element is in focus, the event target will be the body or the root. The event [bubbles](/docs/Learn_web_development/Core/Scripting/Event_bubbling). It can reach Document and Window.

The event target might change between different key events. For example, the `keydown` target for pressing the <kbd>Tab</kbd> key would be different from the `keyup` target, because the focus has changed.

## Syntax

Use the event name in addEventListener():

```js-nolint
addEventListener("keydown", (event) => { })
```

Alternatively, assign to the [event handler property](/docs/Web/API/Document_Object_Model/Events#using_onevent_properties) of the interface:

```js-nolint
onkeydown = (event) => { }
```

## Event type

A KeyboardEvent. Inherits from UIEvent and Event.

KeyboardEvent

## Examples

### addEventListener keydown example

This example logs the KeyboardEvent.code value whenever you press down a key inside the input element.

```html
<input placeholder="Click here, then press down a key." size="40" />
<p id="log"></p>
```

```js
const input = document.querySelector("input");
const log = document.getElementById("log");

input.addEventListener("keydown", logKey);

function logKey(e) {
log.textContent += ` ${e.code}`;
}
```

addEventListener_keydown_example

### keydown events with IME

Since Firefox 65, the `keydown` and [`keyup`](/docs/Web/API/Element/keyup_event) events are now fired during Input method editor composition, to improve cross-browser compatibility for CJKT users ([Firefox bug 354358](https://bugzil.la/354358)). To ignore all `keydown` events that are part of composition, do something like this (229 is a special value set for a `keyCode` relating to an event that has been processed by an IME):

```js
eventTarget.addEventListener("keydown", (event) => {
if (event.isComposing || event.keyCode === 229) {
return;
}
// do something
});
```

> [!NOTE]
> `compositionstart` may fire _after_ `keydown` when typing the first character that opens up the IME, and `compositionend` may fire _before_ `keydown` when typing the last character that closes the IME. In these cases, `isComposing` is false even when the event is part of composition. However, KeyboardEvent.keyCode is still `229` in these cases, so it's still advisable to check `keyCode` as well, although it's deprecated.

## Specifications

## Browser compatibility

## See also

- [`input`](/docs/Web/API/Element/input_event)
- [`keypress`](/docs/Web/API/Element/keypress_event)
- [`keyup`](/docs/Web/API/Element/keyup_event)

source: MDN Web Docs mirror · page

Cite this

"Element: keydown event". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/Element/keydown_event

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·