問(wèn)題描述
有沒(méi)有辦法檢測(cè)一個(gè)鍵當(dāng)前是否在 JavaScript 中被關(guān)閉?
Is there a way to detect if a key is currently down in JavaScript?
我知道keydown"事件,但這不是我需要的.按鍵被按下后的一段時(shí)間,我希望能夠檢測(cè)它是否仍然被按下.
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. 最大的問(wèn)題似乎是在一段時(shí)間后鍵開(kāi)始重復(fù),像惡魔一樣觸發(fā) keydown 和 keyup 事件.希望只有一個(gè)簡(jiǎn)單的 isKeyDown(key) 函數(shù),但如果沒(méi)有,則需要克服/解決這個(gè)問(wèn)題.
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.
推薦答案
有沒(méi)有辦法檢測(cè)一個(gè)鍵當(dāng)前是否在 JavaScript 中被關(guān)閉?
Is there a way to detect if a key is currently down in JavaScript?
不.唯一的可能是監(jiān)控每個(gè) keyup
和 keydown
并記住.
Nope. The only possibility is monitoring each keyup
and keydown
and remembering.
一段時(shí)間后,按鍵開(kāi)始重復(fù),像惡魔一樣觸發(fā) keydown 和 keyup 事件.
after some period of time the key begins to repeat, firing off keydown and keyup events like a fiend.
不應(yīng)該.你肯定會(huì)得到 keypress
重復(fù),并且在許多瀏覽器中你也會(huì)得到重復(fù) keydown
,但是如果 keyup
重復(fù),這是一個(gè)錯(cuò)誤.
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.
不幸的是,這不是一個(gè)完全聞所未聞的錯(cuò)誤:在 Linux、Chromium 和 Firefox 上(當(dāng)它在 GTK+ 下運(yùn)行時(shí),它在流行的發(fā)行版中,如 Ubuntu)都生成重復(fù)的 keyup-keypress-keydown 序列握著鑰匙,這與快速敲擊鑰匙的人無(wú)法區(qū)分.
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.
這篇關(guān)于檢查一個(gè)鍵是否已關(guān)閉?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!