Solved

Detecting keydown and keyup of CTRL

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

734 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