Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 12:52 PM
Mr. x
Guest
 
Posts: n/a
Default capture event when ctrl key is pressed

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
Default 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
Default 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
Default try this...

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

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles