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

AttributeValueDescription
keydownonkeydownFires when a key is pressed down (before character is displayed).
keypressonkeypressFires when a key is pressed (deprecated, use keydown instead).
keyuponkeyupFires when a key is released.

πŸ’» Example 1: keydown Event

Code Example
PREVIEW READY
Loading Editor...
Live Preview

πŸ’» Example 2: keyup Event

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!JS - Form Events