检查一个键是否已关闭?

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?

不.唯一的可能是监控每个 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)