The HTML onkeydown attribute is an event handler that triggers a script to execute when the user presses a key on the keyboard.
Definition and Usage
➔ onkeydown, onkeypress, and onkeyup are all HTML keyboard event attributes.
➔ The sequence of related keyboard events is usually onkeydown, then onkeypress, and then onkeyup.
➔ When the onkeydown event is triggered, an event object can provide information about the key pressed (e.g., event.key, event.code, event.keyCode).
Keyboard key event sequence:
- 1 onkeydown - is triggered when the user presses a key on the keyboard.
- 2 onkeypress - is triggered when the user presses a key on the keyboard (when it produces a character key).
- 3 onkeyup - is triggered when the user releases a key on the keyboard.
Applies to
This attribute can be used on the following element.
| Attribute | Element |
|---|---|
| onkeydown | All visible elements |