admin管理员组文章数量:1279055
I have used the below code, and whenever click the arrow key (left, right, up, down) I get the key value is "0". Can anyone can help on this?
$(document).keypress(function (e) {
alert("key value: " + e.which);
});
How to get (Up, Down, Right, Left) arrow key value when keypress
.
I have used the below code, and whenever click the arrow key (left, right, up, down) I get the key value is "0". Can anyone can help on this?
$(document).keypress(function (e) {
alert("key value: " + e.which);
});
How to get (Up, Down, Right, Left) arrow key value when keypress
.
- possible duplicate of jQuery Keypress Arrow Keys – James Donnelly Commented Nov 21, 2013 at 12:51
2 Answers
Reset to default 5Use keydown
insted of keypress
$(document).keydown(function(event){
var key = event.which;
switch(key) {
case 37:
// Key left.
break;
case 38:
// Key up.
break;
case 39:
// Key right.
break;
case 40:
// Key down.
break;
}
});
In many browsers the keypress
event doesn't recognise the arrow keys (nor keys like "shift", "del", "esc", etc. If you want to capture those key presses, you'll need to use keydown
or keyup
instead:
$(document).keydown(function (e) {
alert("key value: " + e.which);
});
JSFiddle demo.
As for the right, left, up and down arrow keys, those are:
left 37
up 38
right 39
down 40
本文标签:
版权声明:本文标题:javascript - How to get right, left, up and bottom arrow key value in jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741225923a2361856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论