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:


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:

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

function getKeys(event)

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


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


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




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

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


Want to leave a comment?

No problem. Just enter your email and password below.

register | home | reminder

myDesignTool Networking •