Solved

Detecting keydown and keyup of CTRL

Posted on 2006-07-04
5
252 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

770 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