Keyboard and Mouse Events


Users interact with websites using keyboards and mice. JavaScript can detect these actions and respond to them in real time.

Keyboard events help you capture what users type, while mouse events help you react to clicks, movements, and hover actions.

These events are widely used in search boxes, games, forms, menus, and interactive dashboards.

Common Keyboard and Mouse Events

Keyboard Events

  • keydown  – when a key is pressed 
  • keyup    – when a key is released  

Mouse Events

  • click    – when mouse is clicked 
  • dblclick – when mouse is double-clicked
  • mouseover – when mouse moves over an element 
  • mouseout  – when mouse leaves an element  

These events help create smooth user interactions.

This example shows how JavaScript detects which key the user presses.

Such logic is used in live search, form validation, and chat applications.

This example shows how JavaScript responds when the user clicks a button.

Click events are used everywhere — from submitting forms to opening menus.

These examples show how keyboard and mouse events make websites interactive and user-friendly.

By mastering keyboard and mouse events, students gain the ability to build responsive, engaging, and professional web applications.