4

I have a text input, where I need to bind an event on doing a CTRL-V. I have set a global variable named ctrl which is set to 1 whenever a keydown is fired with a which value of 17. Similarly it is made 0 when a keyup is fired with which value of 17

Problem is, there are two CTRL keys. So if I do something like: first pressing the left CTRL key, and while pressing it down, press the right CTRL key also (so that both CTRL keys are pressed now), and then I release only one of them, the keyup is fired and the variable ctrl is set to 0, even though the other CTRL key is still being pressed.

How do I fire the events such that the variable is set to 0 only when both CTRL keys are up (I don't need to exactly differentiate between them).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SexyBeast
  • 7,913
  • 28
  • 108
  • 196

6 Answers6

3

Update : this is now possible in modern browsers

The easiest way to detect left and right control keys in Javascript

$(document).ready(function(){
  $("html").keydown(function(e) {

      if (e.ctrlKey) {
         if (event.location == 1) console.log('left ctrl');
         if (event.location == 2) console.log('right ctrl');
      }

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Note: You have to click the inside white space when you run code snippet to activate keyboard keys. This is tested in Chrome and Safari.

Ibrahim
  • 6,006
  • 3
  • 39
  • 50
2

There are two properties for this of keydown event. You can differentiate left and right Ctrl by using

if ( e.location == 1 || e.keyLocation == 1 ) {
    var keyPosition = 'left';
} else if ( e.location == 2 || e.keyLocation == 2 ) {
    var keyPosition = 'right';
}
Peter
  • 1,674
  • 4
  • 27
  • 44
GingerGear
  • 21
  • 2
0

I don't think there is a way for that unless you write on lowlevel ... keyCode is the same for both (it is 17)

Just You can use e.ctrlKey as a way to determine if the control key was pressed.

However I read around and found one answer mentioning you could do that in IE but I did not try it from my side

Community
  • 1
  • 1
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
0

you can use e.originalEvent.location instead of the global event.location

$(document).ready(function(){
  $("html").keydown(function(e) {

      if (e.ctrlKey) {
         if (e.originalEvent.location === 1) console.log('left ctrl');
         if (e.originalEvent.location === 2) console.log('right ctrl');
      }

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
M.Ali El-Sayed
  • 1,608
  • 20
  • 23
-2

below is your answer for three mouse keyup events. rest for mousewheel you should ask again:

/*
 1 = Left   Mousebutton
 2 = Centre Mousebutton
 3 = Right  Mousebutton
*/

$(document).mousedown(function(e) { 
if (e.which === 3) {
    /* Right Mousebutton was clicked! */
    alert("right key code 3");
}
else if(e.which === 2) {
    alert("Centre key code 2");
}
else if(e.which === 1) {
    alert("Left key code 1");
}
});
Seetpal singh
  • 3,505
  • 1
  • 15
  • 12
-3

you can use this:

$('#inputboxinput').bind('keypress', function(e) {
if(e.keyCode==13){
    // Enter pressed... do anything here...
}

});

the cross-browser way:

if (!event.which && ((event.charCode || event.charCode === 0) ? event.charCode: event.keyCode)) {
    event.which = event.charCode || event.keyCode;
}
Sahil Popli
  • 1,967
  • 14
  • 21
  • Did you even read the question? Besides, jQuery already has `event.which` built-in. You don't need to re-invent it. – JJJ Mar 02 '13 at 10:46
  • Umm, can you explain you answer in a bit more detail? I don't think that will work... – SexyBeast Mar 02 '13 at 10:46