問題描述
有沒有辦法檢測一個鍵當前是否在 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.
這篇關于檢查一個鍵是否已關閉?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!