Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Ctrl / Shift / Alt keys on 'click' event

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"); } }); 
like image 934
Misha Moroshko Avatar asked May 17 '10 06:05

Misha Moroshko


1 Answers

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');       }     }   });  
like image 117
John Hartsock Avatar answered Oct 03 '22 23:10

John Hartsock