Open In App

HTML DOM KeyboardEvent getModifierState() Method

Last Updated : 03 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The KeyboardEvent getModifierState() method in HTML DOM is used to return whether a specified modifier key is pressed, or activated. The KeyboardEvent getModifierState() method returns true if the specified key is pressed, otherwise it returns false. The list of key which is pressed then Modifier keys activated are given below:

  • Alt
  • AltGraph
  • Control
  • Meta
  • Shift

Modifier keys are activated when the user clicks and deactivated by using clicked once again:

  • CapsLock
  • NumLock
  • ScrollLock

Syntax:

event.getModifierState( modifierKey )

Example: In this example, find whether or not the “SHIFT” key is being pressed down.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        HTML DOM KeyboardEvent getModifierState() Method
    </title>
</head>

<body>
    <h2>
        KeyboardEvent getModifierState() Method
    </h2>

    <p>
        Check whether the SHIFT key
        pressed down or not
    </p>

    <input type="text" 
           size="20" 
           onkeydown="keyboard(event)">

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

    <script>
        function keyboard(event) {
            let s = event.getModifierState("Shift");
            document.getElementById("test").innerHTML
                = "Is SHIFT being pressed: " + s;
        }
    </script>
</body>

</html>

Output:

shiftKey

is shift key pressed?

Supported Browsers:

The browser supported by KeyboardEvent getModifierState() Method are listed below:

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

Next Article

Similar Reads