Combining keycode events in jQuery

大兔子大兔子 提交于 2019-12-06 07:39:07

Checking e.keyCode == 16 && e.keyCode == 188 won't do anything useful because e.keyCode won't be both 16 and 188 at the same time. The && operator is a logical conjunction so it evaluates to true if the expressions on both sides of it are true.

I think you want to look at e.shiftKey; if that's true then the Shift key is down, if e.shiftKey is false then Shift is not down:

if(e.shiftKey && e.keyCode == 191) {
    // ?
}
else if(!e.shiftKey && e.keyCode == 191) {
    // /
}

Because each key creates its own keydown event, it will never be possible for keyCode to equal both 16 and 188.

Luckily the SHIFT state is stored in event.shiftKey :)

try this

$(document).keydown(function(e) {
  if (e.shiftKey){
     if (e.which == 188) { 
         alert('both');
     }

  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!