Open In App

Javascript MouseEvent getModifierState() Method

Last Updated : 30 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The mouseEvent.getModifierState() method is used to determine the state of a specified modifier key at the time an event occurs. It returns true if the specified modifier key is being pressed, and false if it is not.

The following modifier keys are active only while they are being pressed:

  • Alt
  • Control
  • Meta
  • Shift

Additionally, some modifier keys toggle their state when clicked: CapsLock, ScrollLock, and NumLock. These keys remain active after being clicked and are deactivated only when clicked again.

Syntax:

event.getModifierState(key)

Parameters:

  • key: It refers to the modifier key. It is case sensitive.

Return Value:

It returns a boolean value that indicates whether the specified modifier key is activated or not.

  • true: it indicates that the specified modifier key is pressed or activated.
  • false: it indicates that the specified modifier key is not pressed.

Example: In this example if CapsLock is activated then true will be displayed, otherwise, false. To see the effect click on the input element after switching CapsLock on/off.

html
<!DOCTYPE html>
<html>

<head>
    <title>JavaScript Mouse Event</title>
</head>

<body style="text-align:center;">

    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h2>
        mouseEvent getModifierState() Method
    </h2>

    Input: <input type="text" onmousedown="geek(event)">

    <p id="p"></p>

    <script>
        function geek(event) {
            var doc = event.getModifierState("CapsLock");
            document.getElementById("p").innerHTML = 
              "Caps Lock activated: " + doc;
        }
    </script>
</body>

</html>

Output:

Supported Browsers:

The browser supported by getModifierState() method are listed below:

  • Apple Safari 10.1
  • Google Chrome 30.0
  • Firefox 15.0
  • Opera 17.0


Next Article

Similar Reads