当前,当在jquery中使用.keypress
检测某个键是否被按住时,它会检测该键是否在输入字段中键入,因为它输入了一个键,等待了一会儿,然后迅速触发了该键(理想的结果)
https://jsfiddle.net/pk2dLxuy/1/是我的小提琴
$(document).keypress(function(keydetect) {
if(keydetect.which == 119) {
console.log("w")
$("div").css("marginTop","-=10")
}
if(keydetect.which == 97) {
console.log("a")
$("div").css("marginLeft","-=10")
}
if(keydetect.which == 115) {
console.log("s")
$("div").css("marginTop","+=10")
}
if(keydetect.which == 100) {
console.log("d")
$("div").css("marginLeft","+=10")
}
}); //this is the bulk of the code, the other html and css dont really matter
当前,如果在一个领域中键入红色方块,则该红色方块会相对于它们移动,我该怎么做,以便在按下某个键时它始终在移动?
在https://v6p9d9t4.ssl.hwcdn.net/html/131423/index.html上可以看到这种情况的示例是可以测试键盘按钮的功能,当您按住一个键时,它会一直响应,而不仅仅是持续一秒钟。 >
我知道可以用另一种编程语言来完成此操作,但这只是为了展示理想效果的想法。
感谢您的帮助。