Skip to content

JavaScript Event buttons

The Event interface represents any event that occurs in the DOM; Some are generated by the user (such as mouse or keyboard events), others are produced by APIs (such as events that show whether an animation is working, videos are paused). There are many types of events, some of which use other interfaces based on the main Event interface. The event itself includes features and methods common to all events.

  • event.type: Returns the name of the event type when it is triggered.
  • event.currentTarget: The event listeners return the item that triggered the event.
  • event.target: Returns the element that triggered the event.
  • button: The read-only property indicates which mouse button is pressed to trigger the event.
  • The buttons: read-only property indicates which buttons are pressed on the mouse (or other input device) when the event is triggered.
  • event.timeStamp: Indicates the time that indicates when the event occurred.
  • event.clientX: Returns the horizontal coordinate of the mouse pointer over the current window when the mouse event is triggered.
  • event.clientY: Returns the vertical coordinate of the mouse pointer relative to the current window when the mouse event is triggered.
  • event.pageX:Returns the horizontal coordinate of the mouse pointer relative to the document when the mouse event is triggered.
  • event.pageY: Returns the vertical coordinate of the mouse pointer relative to the document when the mouse event is triggered
  • altKey: Returns whether the “ALT” key is pressed when a key event is triggered.
  • ctrlKey: Returns whether the “CTRL” key is pressed when a key event is triggered.
  • shiftKey: Returns whether the “SHIFT” key is pressed when a key event is triggered.

DOM Events buttons

The Buttons property returns a number that indicates which mouse button or mouse buttons are pressed when the mouse event is triggered.

When the mouse event occurs, a number (usually the example: “Scanner Back”) button represents a Number representing one or more mouse buttons. If more than one button is pressed, the values are combined to produce a new number. (For example, if the left button (1) and right button (2) are pressed, the returned value is 1 + 2 = 3)

A number representing one or more buttons. When several keys are pressed at the same time, the values are combined.

Possible values:

1: Left mouse button
2: Right mouse button
4: Wheel button or middle button
8: Fourth mouse button (usually “Scanner Back” button)
16: Fifth mouse button (typically “Next” button)

Syntax:

event.buttons


buttons Example

Example 1

buttons example
<body>

    <div id="box" onmousedown="myFunction(event)"></div>
    <div id="show"></div>

    <script>

        function myFunction(event) {
            let but = event.buttons;
            let txt = document.getElementById("show");
            if (but == 1) {
                txt.innerHTML = "The left mouse button";
            }
            else if (but == 2) {
                txt.innerHTML = "The right mouse button";
            }
            else if (but == 3) {
                txt.innerHTML = "The middle mouse button";
            }
            else if (but == 4) {
                txt.innerHTML = "The fourth mouse button";
            }
            else if (but == 5) {
                txt.innerHTML = "The fifth mouse button ";
            }
        }

    </script>

</body>

6 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: