在JavaScript中检测箭头键 [英] Detecting arrow key presses in JavaScript

查看:204
本文介绍了在JavaScript中检测箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测何时按下其中一个箭头键?我用这个来找出:

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

推荐答案

箭头键仅由 onkeydown 触发,而不是 onkeypress

arrow keys are only triggered by onkeydown, not onkeypress

密钥代码是:


  • left = 37

  • up = 38

  • right = 39

  • down = 40

  • left = 37
  • up = 38
  • right = 39
  • down = 40

这篇关于在JavaScript中检测箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆