views:

616

answers:

3

How could I identify which Ctrl / Shift / Alt keys are pressed in the following code ?

$("#my_id").click(function() {
    if (<left control key is pressed>) { alert("Left Ctrl"); }
    if (<right shift and left alt keys are pressed>) { alert("Right Shift + Left Alt"); }
});

I work with Firefox 3.6.3.

Thanks !

+15  A: 
$('#someelement').bind('click', function(event){
   if(event.ctrlKey)
      console.log('ctrl');
   if(event.altKey)
      console.log('alt');
   if(event.shiftKey)
      console.log('shift');

});

I don't know if it's possible to check for left/right keys within a click event, but I don't think it's possible.

jAndy
There must be some way...
Misha Moroshko
Shift left and right both have a key code of 16. Same with Ctrl (17) and alt (18)
I think you'll have to wait for DOM3 support to be able to tell left from right. DOM 3 introduces event.keyLocation (http://www.w3.org/TR/DOM-Level-3-Events/#events-Events-KeyboardEvent-keylocation).
DaveS
Is that possible that Firefox 3.6.3 has some implementation of this ?
Misha Moroshko
It seems those left/right features aren't yet implemented in Firefox. For shift there'sa feature request you could star: https://bugzilla.mozilla.org/show_bug.cgi?id=458433ctrlLeft doesn't even have a bug/feature request.Quirksmode page (though lacking info on any recent browsers): http://www.quirksmode.org/dom/w3c_events.html#keyprops
Simon B.
+2  A: 

Well you this wont work in all browsers just IE 8. Microsoft implemented the ability to determine which (right/left) key was pressed. Here is a link http://msdn.microsoft.com/en-us/library/ms534630(VS.85).aspx

I also found this wonder article about keypress, keyup, keydown event in browsers. http://unixpapa.com/js/key.html

$('#someelement').bind('click', function(event){ 

    if(event.ctrlKey) {
      if (event.ctrlLeft) {
        console.log('ctrl-left'); 
      }
      else {
        console.log('ctrl-right');
      }
    }
    if(event.altKey) {
      if (event.altLeft) {
        console.log('alt-left'); 
      }
      else {
        console.log('alt-right');
      }
    }
    if(event.shiftKey) {
      if (event.shiftLeft) {
        console.log('shift-left'); 
      }
      else
      {
        console.log('shift-right');
      }
    }
  }); 
John Hartsock
Sorry, I didn't mention that I'm interested in Firefox 3.6.3. I updated the question accordingly.
Misha Moroshko
Misha Moroshko. Sorry but there is just no way to do this in firefox yet. Perhaps it will implement "keyLocation", "keyIdentifier", or "shiftLeft" in the future....I do have a wild suggestion but not sure if it will work. I know JAVA applets have the ability to detect right and left keystrokes. if you could capture the keystroke in the applet and then pass the right/left information back to the website. I cant even begin to tell you how to do this. but its an idea. Flash may be an alternative to a JAVA applet
John Hartsock
This is really crazy idea. I don't want to go so far with this... :)
Misha Moroshko
Yeah i figured you wouldnt. But right now its just not possible to do uisng JQuery/HTML DOM and Firefox 3.6.3
John Hartsock
Likewise, you could do it with some [Flash Actionscript3 as well](http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/events/KeyboardEvent.html#keyLocation), but I'm guessing you don't want to go there either.
mVChr
The quirksmode link in one of the other comments suggests that it's IE6+ rather than just IE8...
Stobor
In fact, your own MSDN link states that it's IE5.5+...
Stobor
good pont stobor but since he is looking for a solution in Firefox 3.6.3. That is a bit irrelevent
John Hartsock
A: 

Use js-hotkeys. It is a jQuery plugin.

This is a test to show what you are looking for. It also shows you how to capture left, right, up, down keys standard and those from numeric key pad (the one with numbers 2,4,6,8)! http://afro.systems.googlepages.com/test-static-08.html