CancelImage Upload

How to detect when the Alt key is pressed down with JavaScript

If you are developing a web application, and require the service of some special keyboard combinations, you may wish to bring the Alt key into play. Here then is a tutorial on how the Alt key signal can be detected with JavaScript.

To accomplish this task we will need to question the object event as to the presence of a signal from the Alt key. We will do this using the following code:

event.altKey

If true is returned a signal is present. On the other hand if there is not signal the object will return false. Here is what this code looks like in practice:

<html>
<head>
<title>Detecting when the Alt key is pressed with JavaScript</title>
<script type="text/javascript">

function getKeys(event)
{

         var status = document.getElementById("status");

         if(event.altKey)
         {

                  status.innerHTML = "The Alt key was pressed!";

         }
         else
         {

                  status.innerHTML = "The Alt key was NOT pressed!";

         }

}

</script>
</head>

<body onkeydown="javascript:getKeys(event);">

<span id="status"></span>

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com