HTML | DOM KeyboardEvent getModifierState() Method

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 activated when the user clicked and deactivated by using clicked once again:

  • CapsLock
  • NumLock
  • ScrollLock

Syntax:

event.getModifierState( modifierKey )

Below program illustrates the KeyboardEvent getModifierState() Property in HTML DOM:

Example: This example find out whether or not the “SHIFT” key is being pressed down.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head
    <title>
        HTML DOM KeyboardEvent getModifierState() Method
    </title
</head>
  
<body>
    <h1>GeeksforGeeks</h1
      
    <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) {
            var s = event.getModifierState("Shift");
            document.getElementById("test").innerHTML
                = "Is SHIFT being pressed: " + s;
        }
    </script>
</body>
  
</html>                                               

chevron_right


Output:
Before pressing the button:

After pressing the button:

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

  • Google Chrome 30.0
  • Internet Explorer 9.0
  • Firefox 15.0
  • Safari 10.1
  • Opera 17.0


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.