检查钥匙是否关闭? [英] Check if a key is down?

查看:112
本文介绍了检查钥匙是否关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测JavaScript当前是否存在关键字?



我知道keydown事件,但这不是我需要的。按键后一段时间,我希望能够检测到它是否仍然按下。



P。 S.最大的问题似乎是,在一段时间后,密钥开始重复,启动keydown和keyup事件,如恶魔。希望只有一个简单的isKeyDown(key)函数,但如果没有,则需要克服/解决这个问题。

解决方案

< blockquote>

有没有办法检测JavaScript当前是否存在关键字?


不是。唯一的可能是监控每个 keyup keydown 并记住。


一段时间后,密钥开始重复,启动keydown和keyup事件,如恶魔。


不应该。你肯定会重复 keypress ,在许多浏览器中你也会重复 keydown ,但是如果 keyup 重复,这是一个错误。



不幸的是,这不是一个完全闻所未闻的错误:Linux,Chromium和Firefox(当它在GTK +下运行时,它在流行的发行版中,如Ubuntu)都会为持有的密钥生成重复的keyup-keypress-keydown序列,这是不可能与真正快速敲击密钥的人区分开来的。


Is there a way to detect if a key is currently down in JavaScript?

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

解决方案

Is there a way to detect if a key is currently down in JavaScript?

Nope. The only possibility is monitoring each keyup and keydown and remembering.

after some period of time the key begins to repeat, firing off keydown and keyup events like a fiend.

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.

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.

这篇关于检查钥匙是否关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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