1

I want catch an event for Alt+c or something like that. My code is html

<input type="text" id="name"/>

JavaScript

$("#name").keydown(function(e) {
    if(e.keyCode == 67 && e.keyCode == 18){alert(e.keyCode);} 
});

where is the problem? How it works on both Chrome & firefox?

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
Iftakharul Alam
  • 3,201
  • 4
  • 21
  • 33
  • I think you need to look into the `e.altKey` and `e.ctrlKey`. Try the answers in here http://stackoverflow.com/questions/93695/best-cross-browser-method-to-capture-ctrls-with-jquery or http://stackoverflow.com/questions/4604057/jquery-keypress-ctrlc-or-some-combo-like-that – mr rogers Feb 11 '15 at 16:03
  • I understand your confusion, but if you think about it, `e.keyCode` can't be two different values at the same time. So if you need to check for two keypresses at once, you'll need a different mechanism. That's what `e.altKey` is for. – Katie Kilian Feb 11 '15 at 16:07

3 Answers3

4

You need to check for e.altKey instead:

if(e.altKey && e.keyCode == 67){alert(e.keyCode);} 
antyrat
  • 27,479
  • 9
  • 75
  • 76
3

Basically, you are checking for two codes as the same time. The event (e) has several values you can work with ... including altKey which is a boolean (true or false) ...

Try ... watching the e.altKey and the e.keyCode values.

$("#name").keydown(function(e) {
    if(e.altKey && e.keyCode == 67) {
        alert(e.keyCode);
    } 
});

With the right version of jQuery, there should be no issue between browsers.

rfornal
  • 5,072
  • 5
  • 30
  • 42
0
$(document).keydown(function(e) {
//console.log(e.keyCode); If you want to check other keys code
if(e.keyCode == 67 || e.keyCode == 18){
console.log("alt or c pressed");
}
});

You can work around this to check if the two keys are pressed at the same time. I sujest you to use an aux var set to zero wich increase his value when keydown event triggered and decrease it when keyup.