admin管理员组文章数量:1134554
I'm attempting to capture arrow key presses in jQuery, but no events are being triggered.
$(function(){
$('html').keypress(function(e){
console.log(e);
});
});
This generates events for alphanumeric keys, but delete, arrow keys, etc generate no event.
What am I doing wrong to not be capturing those?
I'm attempting to capture arrow key presses in jQuery, but no events are being triggered.
$(function(){
$('html').keypress(function(e){
console.log(e);
});
});
This generates events for alphanumeric keys, but delete, arrow keys, etc generate no event.
What am I doing wrong to not be capturing those?
Share Improve this question asked Oct 13, 2013 at 16:08 RedBassettRedBassett 3,5873 gold badges35 silver badges59 bronze badges 06 Answers
Reset to default 195You should use .keydown()
because .keypress()
will ignore "Arrows", for catching the key type use e.which
Press the result screen to focus (bottom right on fiddle screen) and then press arrow keys to see it work.
Notes:
.keypress()
will never be fired with Shift, Esc, and Delete but.keydown()
will.- Actually
.keypress()
in some browser will be triggered by arrow keys but its not cross-browser so its more reliable to use.keydown()
.
More useful information
- You can use
.which
Or.keyCode
of the event object - Some browsers won't support one of them but when using jQuery its safe to use the both since jQuery standardizes things. (I prefer.which
never had a problem with). - To detect a
ctrl | alt | shift | META
press with the actual captured key you should check the following properties of the event object - They will be set to TRUE if they were pressed:event.ctrlKey
- ctrlevent.altKey
- altevent.shiftKey
- shiftevent.metaKey
- META ( Command ⌘ OR Windows Key )
Finally - here are some useful key codes ( For a full list - keycode-cheatsheet ):
- Enter: 13
- Up: 38
- Down: 40
- Right: 39
- Left: 37
- Esc: 27
- SpaceBar: 32
- Ctrl: 17
- Alt: 18
- Shift: 16
$(document).keydown(function(e) {
console.log(e.keyCode);
});
Keypress events do detect arrow keys, but not in all browsers. So it's better to use keydown.
These are keycodes you should be getting in your console log:
- left = 37
- up = 38
- right = 39
- down = 40
You can check wether an arrow key is pressed by:
$(document).keydown(function(e){
if (e.keyCode > 36 && e.keyCode < 41)
alert( "arrowkey pressed" );
});
jsfiddle demo
left = 37,up = 38, right = 39,down = 40
$(document).keydown(function(e) {
switch(e.which) {
case 37:
$( "#prev" ).click();
break;
case 38:
$( "#prev" ).click();
break;
case 39:
$( "#next" ).click();
break;
case 40:
$( "#next" ).click();
break;
default: return;
}
e.preventDefault();
});
Please refer the link from JQuery
http://api.jquery.com/keypress/
It says
The keypress event is sent to an element when the browser registers keyboard input. This is similar to the keydown event, except that modifier and non-printing keys such as Shift, Esc, and delete trigger keydown events but not keypress events. Other differences between the two events may arise depending on platform and browser.
That means you can not use keypress in case of arrows.
$(document).on( "keydown", keyPressed);
function keyPressed (e){
e = e || window.e;
var newchar = e.which || e.keyCode;
alert(newchar)
}
本文标签: javascriptjQuery Keypress Arrow KeysStack Overflow
版权声明:本文标题:javascript - jQuery Keypress Arrow Keys - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736843632a1955208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论