Nearby lessons
31 of 40πΉ JavaScript Keyboard Events
π What are JavaScript Keyboard Events?
Keyboard events are triggered when a user interacts with the keyboard, such as pressing or releasing keys. JavaScript provides event handlers to detect and respond to these events in real-time. π
These events are commonly used in form validations, keyboard shortcuts, and games.
π Common Keyboard Events
| Attribute | Value | Description |
|---|---|---|
keydown | onkeydown | Fires when a key is pressed down (before character is displayed). |
keypress | onkeypress | Fires when a key is pressed (deprecated, use keydown instead). |
keyup | onkeyup | Fires when a key is released. |
π» Example 1: keydown Event
Code Example
PREVIEW READY
π» Example 2: keyup Event
Code Example
PREVIEW READY
π§ Test Your Knowledge
5 QuestionsProgress: 0 / 5
Keep Going!JS - Form Events