The HTML onkeypress attribute is an event handler that triggers a script to execute when the user presses a key on the keyboard.
Definition and Usage
➔ onkeypress, onkeydown and onkeyup are all HTML keyboard event attributes.
➔ The sequence of related keyboard events is usually onkeydown, then onkeypress, and then onkeyup.
➔ It is only activated when it produces a character key that includes alphanumeric keys, punctuation keys, and the spacebar. It does not fire for modifier keys like Shift, Ctrl, Alt or special keys like Esc.
➔ When the onkeypress 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 |
|---|---|
| onkeypress | All visible elements |