最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to capture ALT+C keypress - Stack Overflow

programmeradmin1浏览0评论

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 and e.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 what e.altKey is for. – Katie Kilian Commented Feb 11, 2015 at 16:07
Add a ment  | 

3 Answers 3

Reset to default 4

You 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.

发布评论

评论列表(0)

  1. 暂无评论