Master Keyboard and Mouse Events in JavaScript: A Practical Guide

I will explore how to listen for keyboard and mouse action so you can make your web page interactive. I am having a heading, an input whose id is text-input, a div which is a hover box with the text Hover me, and a paragraph which is blank but we will fill it with the help of JavaScript.

I also have a small CSS part for this hover box with width, height, background color, margin, text alignment, padding, and font weight.

Here is the plan:

  • First, select the elements with document.getElementById.
  • Deal with the keyboard event using keyup to mirror what you type into the paragraph.
  • Deal with the mouse events using mouseenter and mouseleave to change the box style and text.
  • Add a click event to show an alert when the hover box is clicked.

Setup for JavaScript Keyboard and Mouse Events

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 1

 

HTML structure:

  • A heading
  • An input with id text-input
  • A div with id hover-box that shows Hover me
  • A paragraph with id output
  • A script tag to connect JavaScript

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 9

 

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 7

 

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 3

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 2

CSS for the hover box:

  • Width, height
  • Background color
  • Margin
  • Text alignment
  • Padding
  • Font weight

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 6

 

Markup and basic styles

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 8

 

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>JavaScript Keyboard and Mouse Events</title>
    <style>
      .hover-box {
        width: 200px;
        height: 120px;
        background: lightblue;
        margin: 16px 0;
        text-align: center;
        padding: 12px;
        font-weight: 600;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        user-select: none;
      }
    </style>
  </head>
  <body>
    <h1>Events demo</h1>
    <input id="text-input" type="text" placeholder="Type here" />
    <div id="hover-box" class="hover-box">Hover me</div>
    <p id="output"></p>

    <script src="app.js"></script>
  </body>
</html>

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 5

 

Selecting elements for JavaScript Keyboard and Mouse Events

 

First, select the items. I am having this const textInput that is document.getElementById(‘text-input’).

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 10

This is the particular input and that we have selected with the help of document.getElementById. The same we have done for the hover box and the output also.

const textInput = document.getElementById('text-input');
const hoverBox = document.getElementById('hover-box');
const output = document.getElementById('output');

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 11

 

Keyboard: keyup in JavaScript Keyboard and Mouse Events

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 12

 

We are going to deal with the keyboard event. For the keyboard event the event is keyup.

  • Whatever you have entered inside the text input is stored in e.
  • Use e.target to get the particular input field.
  • Use e.target.value to get the actual value written inside the input field.
  • Set output.innerText to show what you typed.

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 17

 

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 16

 

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 15

 

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 14

 

textInput.addEventListener('keyup', e => {
  output.innerText = `You typed: ${e.target.value}`;
});

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 13

 

Mouse: mouseenter and mouseleave in JavaScript Keyboard and Mouse Events

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 18

 

We will do two different things: first for mouseenter and then for mouseleave.

  • On mouseenter, change the background to light green and change the inner text to Hello.
  • On mouseleave, change the color back to light blue like earlier and the text to Hover me.

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 20

 

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 19

 

hoverBox.addEventListener('mouseenter', () => {
  hoverBox.style.background = 'lightgreen';
  hoverBox.innerText = 'Hello';
});
hoverBox.addEventListener('mouseleave', () => {
  hoverBox.style.background = 'lightblue';
  hoverBox.innerText = 'Hover me';
});

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 21

 

Mouse: click in JavaScript Keyboard and Mouse Events

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 22

 

When I click on that particular hover box, I want an alert to come.

hoverBox.addEventListener('click', () => {
  alert('You clicked the box');
});

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 23

 

Full working example for JavaScript Keyboard and Mouse Events

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 24

 

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>JavaScript Keyboard and Mouse Events</title>
    <style>
      .hover-box {
        width: 200px;
        height: 120px;
        background: lightblue;
        margin: 16px 0;
        text-align: center;
        padding: 12px;
        font-weight: 600;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        user-select: none;
      }
    </style>
  </head>
  <body>
    <h1>Events demo</h1>
    <input id="text-input" type="text" placeholder="Type here" />
    <div id="hover-box" class="hover-box">Hover me</div>
    <p id="output"></p>
    <script>
      const textInput = document.getElementById('text-input');
      const hoverBox = document.getElementById('hover-box');
      const output = document.getElementById('output');
      // Keyboard: keyup
      textInput.addEventListener('keyup', e => {
        output.innerText = `You typed: ${e.target.value}`;
      });
      // Mouse: enter and leave
      hoverBox.addEventListener('mouseenter', () => {
        hoverBox.style.background = 'lightgreen';
        hoverBox.innerText = 'Hello';
      });
      hoverBox.addEventListener('mouseleave', () => {
        hoverBox.style.background = 'lightblue';
        hoverBox.innerText = 'Hover me';
      });
      // Mouse: click
      hoverBox.addEventListener('click', () => {
        alert('You clicked the box');
      });
    </script>
  </body>
</html>

 

Master Keyboard and Mouse Events in JavaScript: A Practical Guide screenshot 25

 

Final Thoughts

By this we deal with the mouse and the keyboard events in JavaScript. Keyup updates the paragraph with what you type using e.target.value, mouseenter and mouseleave change styles and content, and click triggers an alert. This simple setup shows how to make an interactive page by listening to user actions.

Leave a Comment