Connecting Tech Pros Worldwide Help | Site Map

capture event when ctrl key is pressed

  #1  
Old July 20th, 2005, 12:52 PM
Mr. x
Guest
 
Posts: n/a
Hello,
I want to capture the event when the <ctrl> key is pressed.

How can I do that ?

Thanks :)


  #2  
Old July 20th, 2005, 12:52 PM
Michael Winter
Guest
 
Posts: n/a

re: capture event when ctrl key is pressed


"Mr. x" wrote on 14/11/2003:
[color=blue]
> Hello,
> I want to capture the event when the <ctrl> key is pressed.
>
> How can I do that ?[/color]

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)


  #3  
Old July 20th, 2005, 12:53 PM
Fabian
Guest
 
Posts: n/a

re: capture event when ctrl key is pressed


Michael Winter hu kiteb:
[color=blue]
> "Mr. x" wrote on 14/11/2003:
>[color=green]
>> Hello,
>> I want to capture the event when the <ctrl> key is pressed.
>>
>> How can I do that ?[/color]
>
> 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).[/color]

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

  #4  
Old May 4th, 2006, 06:07 PM
Newbie
 
Join Date: May 2006
Posts: 1

re: capture event when ctrl key is pressed


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;"
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Capturing Modifier Keys in ProcessCmdKey Event Phil Galey answers 2 November 21st, 2005 10:41 PM
How to capture tab key presses Don answers 2 November 21st, 2005 05:31 PM
Capturing a TAB key press event supster answers 9 November 17th, 2005 12:20 AM
keycode for ctrl ?? oeyvind toft answers 8 July 20th, 2005 10:48 AM