在 JavaScript 中检测箭头键按下
如何检测何时按下方向键之一?我用这个来找出:
function checkKey(e) {
var event = window.event ? window.event : e;
console.log(event.keyCode)
}
虽然它适用于所有其他键,但不适用于箭头键(可能是因为浏览器默认情况下应该在这些键上滚动)。
How do I detect when one of the arrow keys are pressed? I used this to find out:
function checkKey(e) {
var event = window.event ? window.event : e;
console.log(event.keyCode)
}
Though it worked for every other key, it didn't for arrow keys (maybe because the browser is supposed to scroll on these keys by default).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
箭头键仅由
onkeydown
触发,而不是由onkeypress
触发。键码为:
Arrow keys are only triggered by
onkeydown
, notonkeypress
.The keycodes are:
event.key === "ArrowRight"...
更新且更清晰:使用
event.key
。不再有任意数字代码!如果您正在进行编译或知道您的用户都使用现代浏览器,请使用它!详细处理:
现代开关处理:
"w", "a", "s", "d"
作为方向,使用event.code
为了支持使用非 qwerty/英文键盘布局的用户,您应该使用
event.code
。即使最终的字符发生变化,这也将保留物理键位置。event.key
在 Dvorak 上将是 ,,在 Azerty 上将是 z,从而使您的游戏无法玩。最理想的是,您还允许按键重新映射,这对玩家有利,无论他们的情况如何。
PS
event.code
与箭头相同key
Mozilla 文档代码
Mozilla 文档支持的浏览器
event.key === "ArrowRight"...
More recent and much cleaner: use
event.key
. No more arbitrary number codes! If you are transpiling or know your users are all on modern browsers, use this!Verbose Handling:
Modern Switch Handling:
"w", "a", "s", "d"
for direction, useevent.code
To support users who are using non-qwerty/English keyboard layouts, you should instead use
event.code
. This will preserve physical key location, even if resulting character changes.event.key
would be , on Dvorak and z on Azerty, making your game unplayable.Optimally, you also allow key remapping, which benefits the player regardless of their situation.
P.S.
event.code
is the same for arrowskey
Mozilla Docscode
Mozilla DocsSupported Browsers
按键上下通话功能。每个键都有不同的代码。
On key up and down call function. There are different codes for each key.
可能是最简洁的表述:
演示(感谢用户 Angus Grant): http://jsfiddle.net/angusgrant/E3tE6/
这应该可以跨浏览器工作。如果有浏览器不工作的地方请发表评论。
还有其他方法可以获取关键代码(e.which、e.charCode 和 window.event 而不是 e),但它们不是必需的。您可以在 http://www.asquare.net/javascript/tests/KeyCode.html< 尝试其中的大部分内容< /a>.
请注意,event.keycode 不能与 Firefox 中的 onkeypress 配合使用,但可以与 onkeydown 配合使用。
Possibly the tersest formulation:
Demo (thanks to user Angus Grant): http://jsfiddle.net/angusgrant/E3tE6/
This should work cross-browser. Leave a comment if there is a browser where it does not work.
There are other ways to get the key code (e.which, e.charCode, and window.event instead of e), but they should not be necessary. You can try most of them out at http://www.asquare.net/javascript/tests/KeyCode.html.
Note that event.keycode does not work with onkeypress in Firefox, but it does work with onkeydown.
对于不可打印的按键(例如箭头键),请使用
keydown
,而不是keypress
:我发现的最佳 JavaScript 按键事件参考(例如,击败怪异模式)是这里:http://unixpapa.com/js/key.html
Use
keydown
, notkeypress
for non-printable keys such as arrow keys:The best JavaScript key event reference I've found (beating the pants off quirksmode, for example) is here: http://unixpapa.com/js/key.html
自 keyCode 现已弃用以来的现代答案< /strong> 支持 key< /强>:
Modern answer since keyCode is now deprecated in favor of key:
我相信最新的方法是:
这假设开发人员希望代码在页面上的任何位置都处于活动状态,并且客户端应该忽略任何其他按键。消除event.preventDefault();如果按键(包括此处理程序捕获的按键)应该仍然处于活动状态,则行。
I believe the most recent method would be:
This assumes the developer wants the code to be active anywhere on the page and the client should ignore any other key presses. Eliminate the event.preventDefault(); line if keypresses, including those caught by this handler should still be active.
这是一个示例实现:
Here's an example implementation:
我是这样做的:
Here's how I did it:
箭头键在 keyup 上触发
onkeydown 允许 ctrl、alt、shit
onkeyup 允许 tab、向上箭头、向下箭头、向左箭头、向下箭头
Arrow Keys are triggered on keyup
onkeydown allows ctrl, alt, shits
onkeyup allows tab, up arrows, down arrows, left arrows, down arrows
我已经能够用 jQuery 捕获它们:
示例: http://jsfiddle.net/AjKjU/
I've been able to trap them with jQuery:
An example: http://jsfiddle.net/AjKjU/
这是 chrome 和 firefox 的工作代码
That is the working code for chrome and firefox
我也在寻找这个答案,直到我看到这篇文章。
我找到了另一种解决方案来了解不同键的键码,以解决我的问题。我只是想分享我的解决方案。
只需使用 keyup/keydown 事件在控制台/警报中写入相同的值,使用
event.keyCode
。就像--rupam
I was also looking for this answer until I came across this post.
I've found another solution to know the keycode of the different keys, courtesy to my problem. I just wanted to share my solution.
Just use keyup/keydown event to write the value in the console/alert the same using
event.keyCode
. like-- rupam
那个更短。
函数 IsArrows (e) {
return (e.keyCode >= 37 && e.keyCode <= 40);
}
That's shorter.
function IsArrows (e) {
return (e.keyCode >= 37 && e.keyCode <= 40);
}
这个图书馆太棒了!
https://craig.is/killing/mice
您需要快速按下序列才能突出显示不过该页面中的代码。
This library rocks!
https://craig.is/killing/mice
You need to press the sequence a bit fast to highlight the code in that page though.
使用 key 和 ES6。
这为您提供了每个箭头键的单独功能,而无需使用开关,并且当
NumLock
打开时,还可以使用数字键盘中的 2、4、6、8 键。With key and ES6.
This gives you a separate function for each arrow key without using switch and also works with the 2,4,6,8 keys in the numpad when
NumLock
is on.如果您在 2023 年仍在考虑桌面开发:
KeyboardEvent: keyCode property 已被弃用
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
使用 key 属性
https://developer.mozilla .org/en-US/docs/Web/API/KeyboardEvent/key
In the off chance you're still developing with desktops in mind in 2023:
KeyboardEvent: keyCode property has been deprecated
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
Use the key property
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
重新回答您需要
keydown
而不是keypress
。假设您想在按下按键时连续移动某些东西,我发现
keydown
适用于除 Opera 之外的所有浏览器。对于 Opera,keydown
仅在第一次按下时触发。为了适应 Opera 使用:Re answers that you need
keydown
notkeypress
.Assuming you want to move something continuously while the key is pressed, I find that
keydown
works for all browsers except Opera. For Opera,keydown
only triggers on 1st press. To accommodate Opera use:如果你使用jquery那么你也可以这样做,
If you use jquery then you can also do like this,
控制键代码
%=37
和&=38
...并且仅箭头键 left=37 up=38control the Key codes
%=37
and&=38
... and only arrow keys left=37 up=38如果你想检测箭头按键但不需要在 Javascript 中指定
If you want to detect arrow keypresses but not need specific in Javascript