检查一个键是否已关闭?

时间:2023-03-15
本文介绍了检查一个键是否已关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有没有办法检测一个键当前是否在 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?

不.唯一的可能是监控每个 keyupkeydown 并记住.

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.

这篇关于检查一个键是否已关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:keypress 和 keyup - 为什么 keyCode 不同? 下一篇:为什么在 keydown 回调中返回 false 不会停止按钮单击事件?

相关文章