Javascript | MouseEvent altKey Property

The mouseEvent altKey property is used to define whether the alt key is pressed or not. It is a boolean value. When alt key is pressed then onclick of the mouse buttons it returns true and if it is not pressed then it returns false.

Syntax:

event.altKey

Return Value: It returns a boolean value indicating whether alt key is pressed or not.



  • true: it indicates alt key is pressed
  • false: it indicates alt key is not pressed

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript Mouse Event</title>
    </head>
    <body style = "text-align:center;">
      
        <h1 style = "color:green;">
            GeeksforGeeks
        </h1>
          
        <h2>
            mouseEvent shiftKey Property
        </h2>
   
        <button onclick="geek (event);">Get Alt key state!</button>        
        <script>
        function geek (event) {
            if (event.altKey) {
                alert ("Alt key is pressed.");
            }
            else {
                alert ("Alt key is not pressed.");
            }
        }
    </script>
    </body>
</html>

chevron_right


Output:
Clicking button (Alt key is not pressed):
altkey
Clicking button (Alt key is pressed):
altkey

Supported Browsers: The browser supported by altkey property are listed below:

  • Apple Safari
  • Google Chrome
  • Firefox
  • Opera
  • Internet Explorer


My Personal Notes arrow_drop_up


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.