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

Example03
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

💻 Example 2: keyup Event

Example04
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5