admin管理员组文章数量:1023273
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?
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?
Share Improve this question edited Mar 6, 2018 at 23:11 L Y E S - C H I O U K H 5,1009 gold badges42 silver badges59 bronze badges asked Feb 11, 2015 at 16:00 Iftakharul AlamIftakharul Alam 3,3214 gold badges24 silver badges33 bronze badges 2-
I think you need to look into the
e.altKey
ande.ctrlKey
. Try the answers in here stackoverflow./questions/93695/… or stackoverflow./questions/4604057/… – mr rogers Commented Feb 11, 2015 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 whate.altKey
is for. – Katie Kilian Commented Feb 11, 2015 at 16:07
3 Answers
Reset to default 4You need to check for e.altKey
instead:
if(e.altKey && e.keyCode == 67){alert(e.keyCode);}
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.
$(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.
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?
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?
Share Improve this question edited Mar 6, 2018 at 23:11 L Y E S - C H I O U K H 5,1009 gold badges42 silver badges59 bronze badges asked Feb 11, 2015 at 16:00 Iftakharul AlamIftakharul Alam 3,3214 gold badges24 silver badges33 bronze badges 2-
I think you need to look into the
e.altKey
ande.ctrlKey
. Try the answers in here stackoverflow./questions/93695/… or stackoverflow./questions/4604057/… – mr rogers Commented Feb 11, 2015 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 whate.altKey
is for. – Katie Kilian Commented Feb 11, 2015 at 16:07
3 Answers
Reset to default 4You need to check for e.altKey
instead:
if(e.altKey && e.keyCode == 67){alert(e.keyCode);}
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.
$(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.
本文标签: javascriptHow to capture ALTC keypressStack Overflow
版权声明:本文标题:javascript - How to capture ALT+C keypress - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745513384a2153928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论