MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Keyboard & Mouse Events0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
4.1Event Listeners4.2Event Bubbling & Capturing4.3Keyboard & Mouse Events4.4Form Events and Validation
HomeTutorialsJavaScript for IntermediateKeyboard & Mouse Events
Form Events and Validation

Keyboard & Mouse Events

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Capturing user input through keyboards, mice, and pointer devices is essential for creating interactive web apps like search dropdowns, modal windows, drag-and-drop interfaces, and gaming loops.


1. Keyboard Events: keydown, keyup, and key

Event Types

  • keydown: Fires immediately when a key is pressed down. Repeats if held down.
  • keyup: Fires when the key is released.
  • (Note: keypress is deprecated and should not be used in modern code).

Key Properties: key vs. code

1window.addEventListener('keydown', (event) => {
2 console.log('Key:', event.key); // Actual character printed (e.g. 'Enter', 'a', 'A', 'Escape')
3 console.log('Code:', event.code); // Physical hardware key code (e.g. 'KeyA', 'Enter', 'Space')
4 console.log('Modifier keys:', {
5 ctrl: event.ctrlKey,
6 alt: event.altKey,
7 shift: event.shiftKey,
8 meta: event.metaKey // Command on Mac, Windows key on PC
9 });
10});

Implementing Keyboard Shortcuts (Modal Escape)

1document.addEventListener('keydown', (event) => {
2 // Close modal on 'Escape'
3 if (event.key === 'Escape') {
4 const activeModal = document.querySelector('.modal.open');
5 if (activeModal) activeModal.classList.remove('open');
6 }
7 
8 // Save shortcut: Ctrl + S or Cmd + S
9 if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 's') {
10 event.preventDefault(); // Prevent browser 'Save Webpage' dialog
11 saveDocument();
12 }
13});

2. Mouse Events: click, dblclick, mousedown, mouseup

1const box = document.querySelector('.interactive-box');
2 
3box.addEventListener('mousedown', (e) => {
4 // e.button: 0 = Main (Left), 1 = Auxiliary (Middle), 2 = Secondary (Right)
5 if (e.button === 0) {
6 console.log('Left mouse button pressed');
7 }
8});
9 
10box.addEventListener('contextmenu', (e) => {
11 e.preventDefault(); // Prevents default browser right-click menu
12 showCustomContextMenu(e.clientX, e.clientY);
13});

3. Mouse Coordinate Systems: client, page, and screen

1 ┌──────────────────────────────────────────────┐ Screen (e.screenX/Y)
2 │ Monitor Display Boundary │
3 │ ┌────────────────────────────────────────┐ │
4 │ │ Browser Window Viewport │ │ Client (e.clientX/Y)
5 │ │ (e.clientX = relative to viewport) │ │
6 │ │ ┌──────────────────────────────────┐ │ │
7 │ │ │ Rendered Web Page Document │ │ │ Page (e.pageX/Y)
8 │ │ │ (e.pageX = includes scroll offset│ │ │
9 │ │ └──────────────────────────────────┘ │ │
10 │ └────────────────────────────────────────┘ │
11 └──────────────────────────────────────────────┘

4. mouseenter / mouseleave vs. mouseover / mouseout

This difference is critical for hover menus and tooltips:

  • mouseover and mouseout: Bubble up through child elements. Moving over a child inside the container fires additional events!
  • mouseenter and mouseleave: Do NOT bubble. They trigger strictly when entering or leaving the target element boundary.
1const dropdown = document.querySelector('.dropdown');
2 
3// Clean hover behavior without child flickering
4dropdown.addEventListener('mouseenter', () => dropdown.classList.add('active'));
5dropdown.addEventListener('mouseleave', () => dropdown.classList.remove('active'));

5. Pointer Events: The Modern Unified Standard

Pointer Events unify Mouse, Touch, and Stylus/Pen interactions into a single API:

1const canvas = document.querySelector('#drawing-canvas');
2 
3canvas.addEventListener('pointerdown', (e) => {
4 console.log(`Input Device: ${e.pointerType}`); // 'mouse', 'touch', or 'pen'
5 console.log(`Pressure: ${e.pressure}`); // Stylus pressure level (0.0 to 1.0)
6});

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which property should you check to inspect the actual semantic character generated by a key press?
Question 2
How do mouseenter and mouseleave differ from mouseover and mouseout?
Question 3
What coordinate property represents mouse position relative to the visible browser viewport?
Question 4
How do you prevent the standard browser context menu from appearing upon right-click?
Question 5
What is the main advantage of the Pointer Events API (pointerdown, pointermove) over traditional Mouse Events?
Next Lesson

Form Events and Validation

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Event Bubbling & CapturingForm Events and Validation

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext