Solved

Detecting keydown and keyup of CTRL

Posted on 2006-07-04
5
256 Views
Last Modified: 2008-03-03
Hi guys,

I want to be able to, when the CTRL key is pressed, run a function, and when the CTRL key is released, run another function.

What code do I need to achieve this?

Cheers!
0
Comment
Question by:Cyber-Drugs
  • 2
  • 2
5 Comments
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 17038796
Here is a code (a cross-browser), which detects the ctrl key event

<html>
<body>
<script language="javascript">
function TrapCtrl(evt)
{

if (!evt) { evt = window.event; }

// If ctrlkey was not used, return...
if (!evt.ctrlKey) { return; }

// Gather the target object and keycode.
var trgObj = null;
var keyCode = null;

if (document.all) {
   keyCode = evt.keyCode;
   trgObj  = evt.srcElement;
}
else {
   keyCode = evt.which;
   trgObj  = evt.target;
}
var key = String.fromCharCode (keyCode);
window.status = 'Ypur Pressed ctrl+' + key;
}
</script>
<form>
<input type="text" onkeydown="TrapCtrl(event);">
</form>
</body>

</html>
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17038911
Hi pravinasar,

Thanks for the code, but unfortunately that only solves half of the problem.

I need to also be able to see when the CTRL key is released.

Basically what I am doing is this...

While the CTRL key is held down, an image is displayed, and once the key is released, the image is hidden once again.

If it helps at all, I only need IE compatability.

Cheers!
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17039444
OK, here's the code I was looking for:

document.onkeydown=checkKeys
function checkKeys(){
      if (event.keyCode==17){
            document.getElementById('information_data').innerHTML = 'Ctrl key pressed';
            return false;
      }
}
document.onkeyup=checkKeysB
function checkKeysB(){
      if (event.keyCode==17){
            document.getElementById('information_data').innerHTML = 'Ctrl key released';
            return false;
      }
}
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 17042740
Good to know, you found solution.

If need to ask for Points Refund, I have no objection.

Thanks,

_PA
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17046717
Closed, 500 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question