Check if a key is down?(检查一个键是否已关闭?)
问题描述
有没有办法检测一个键当前是否在 JavaScript 中被关闭?
Is there a way to detect if a key is currently down in JavaScript?
我知道keydown"事件,但这不是我需要的.按键被按下后的一段时间,我希望能够检测它是否仍然被按下.
I know about the "keydown" event, but that's not what I need. Some time AFTER the key is pressed, I want to be able to detect if it is still pressed down.
P.S. 最大的问题似乎是在一段时间后键开始重复,像恶魔一样触发 keydown 和 keyup 事件.希望只有一个简单的 isKeyDown(key) 函数,但如果没有,则需要克服/解决这个问题.
P. S. The biggest issue seems to be that after some period of time the key begins to repeat, firing off keydown and keyup events like a fiend. Hopefully there is just a simple isKeyDown(key) function, but if not then this issue will need to be overcome / worked around.
推荐答案
有没有办法检测一个键当前是否在 JavaScript 中被关闭?
Is there a way to detect if a key is currently down in JavaScript?
不.唯一的可能是监控每个 keyup
和 keydown
并记住.
Nope. The only possibility is monitoring each keyup
and keydown
and remembering.
一段时间后,按键开始重复,像恶魔一样触发 keydown 和 keyup 事件.
after some period of time the key begins to repeat, firing off keydown and keyup events like a fiend.
不应该.你肯定会得到 keypress
重复,并且在许多浏览器中你也会得到重复 keydown
,但是如果 keyup
重复,这是一个错误.
It shouldn't. You'll definitely get keypress
repeating, and in many browsers you'll also get repeated keydown
, but if keyup
repeats, it's a bug.
不幸的是,这不是一个完全闻所未闻的错误:在 Linux、Chromium 和 Firefox 上(当它在 GTK+ 下运行时,它在流行的发行版中,如 Ubuntu)都生成重复的 keyup-keypress-keydown 序列握着钥匙,这与快速敲击钥匙的人无法区分.
Unfortunately it is not a completely unheard-of bug: on Linux, Chromium, and Firefox (when it is being run under GTK+, which it is in popular distros such as Ubuntu) both generate repeating keyup-keypress-keydown sequences for held keys, which are impossible to distinguish from someone hammering the key really fast.
这篇关于检查一个键是否已关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查一个键是否已关闭?
基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01