473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 :)
Jul 20 '05 #1
3 34305
"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
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
ckohtz
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Boaz Ben-Porat | last post by:
Is there any way to determine if the CTRL key is pressed from a MouseDown event on a DataGrid ? TIA Boaz ben-Porat DataPharm a/s Denmark
1
by: karunakar | last post by:
Hi all Just i want capture the with "Ctrl" (Key bord function) + any F1 or F4 In windows application How can capture the event in C# .net Regards, Venu
5
by: ewillyb | last post by:
Hi, ASP.NET has some interesting behavior when the user hits the Enter key. If there are multiple ASP:Buttons (rendered as HTML submits) on the form, when the user hits enter, the first button's...
7
by: Bob Achgill | last post by:
When I use the code for KeyPress to capture pressing a certain key for processing on a form with no Text Box it works. But when I try the same code on my application that has text boxes it does...
2
by: Don | last post by:
It took me a while to find this code, so I decided to post it here for others who might also be looking for a way to trap tab key presses. You can extend a control and place this code in it to...
4
by: t3projects | last post by:
I'm working on controlling a windows media player I'm trying to figure out how to control it with keys like CTRL+SHIFT+P to play it. The problem is that I don't know how to capture all three at the...
2
by: petedawn | last post by:
guys, i want to capture all the function key presses from F1 to F12. now i want that when the user presses F1 from within the browser i should display a alert window saying that they have...
1
by: savithriarul | last post by:
Hi, I am trying to capture the CTRL + c event and then get the selected value and then capture the CTRL + V event and paste the selected value in the selected range in the fp webform 2.5. any...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.