Solved

Detecting keydown and keyup of CTRL

Posted on 2006-07-04
5
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 29

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 29

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

624 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