CancelImage Upload

Detecting when the CTRL key is pressed for shortcut keys with JavaScript

As a frequent user of various IT applications you are no doubt familiar with shortcut keys. And while these are common among stand alone applications they are rarely useful or necessary on individual websites. This however, may not apply for your web applications, such as intranets, and other client orientated programs.

Here then is how you detect whether or not the shift button was pressed, using JavaScript, to help determine whether or not a shortcut is indeed being called upon:

event.ctrlKey

The key to this code is the object event, which can be called upon whenever an event takes place inside an HTML document. Events are caused through user interaction with the interface, such as mouse movements, clicks, or in this case even through the keyboard.

If the CTRL key is indeed pressed, true will be returned from this object extension. If not we will receive false, or 0, meaning the CTRL signal could not be detected. Here is an example to help further illustrate this:

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

function getKeys(event)
{

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

       if(event.ctrlKey)
       {

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

       }
       else
       {

              status.innerHTML = "The CTRL 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