By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,837 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,837 IT Pros & Developers. It's quick & easy.

capture event when ctrl key is pressed

P: n/a
Hello,
I want to capture the event when the <ctrl> key is pressed.

How can I do that ?

Thanks :)
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Mr. x" wrote on 14/11/2003:
Hello,
I want to capture the event when the <ctrl> key is pressed.

How can I do that ?


This is under IE 6 (I don't have any other browsers, so someone else
will have to chip in).

A onkeydown event is fired when the CTRL key is pressed, but the
event.which and event.modifiers properties are undefined (even when a
letter key is pressed). This means that there is no reliable way to
detect it. The onkeypress event doesn't work with modifier keys
(ctrl, alt, shift, etc).

Do Mac keyboards even have CTRL keys?

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 20 '05 #2

P: n/a
Michael Winter hu kiteb:
"Mr. x" wrote on 14/11/2003:
Hello,
I want to capture the event when the <ctrl> key is pressed.

How can I do that ?


This is under IE 6 (I don't have any other browsers, so someone else
will have to chip in).

A onkeydown event is fired when the CTRL key is pressed, but the
event.which and event.modifiers properties are undefined (even when a
letter key is pressed). This means that there is no reliable way to
detect it. The onkeypress event doesn't work with modifier keys
(ctrl, alt, shift, etc).


Both navigator 7 and explorer 6 can detect the onkeyup event, and do
recognise the control key. onkeydown and onkeypress may also be
supported. However, control+[key] combo keystrokes are rather more
difficult to detect. I haven't tried.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #3

P: 1
This works completely in Mozilla (ns), have problems in IE when only using the ev.ctrlKey && ev.keyCode, it's like it won't capture the ctrl unless the shift or alt key are pressed too. Annoying!

Expand|Select|Wrap|Line Numbers
  1. var captureKeys = function(ev) {
  2.     ev = ev || window.event;             // gets the event in ie or ns
  3.     kCode = ev.keyCode || ev.which;   // gets the keycode in ie or ns
  4.  
  5.     /* in ie, when pressing the ctrl + shift + key, it gives the key code for the capitalized key (probably because shift is pressed) 
  6.        in ns pressing ctrl, shift and another key doesn't change the keycode
  7.        thus, the || and two different numbers */
  8.  
  9.     if (ev.ctrlKey && ev.shiftKey && kCode == 19 || ev.ctrlKey && ev.shiftKey && kCode == 83) {    // ctrl+alt+s
  10.                 saveFunction() // another function that does something
  11.         return false;  // make it so the browser ignores key combo
  12.     }
  13.     if (ev.ctrlKey && kCode == 119) { // ctrl+w
  14.         closeWin();  // run your own script to close the window 
  15.                                  // doesn't work in ie, ie just closes the window
  16.         return false;
  17.     }
  18. }
and then put this in the body tag...

onload="document.onkeypress=captureKeys;"
May 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.