Solved

Detecting keydown and keyup of CTRL

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now