如果您希望以可控方式重复按键,则必须自己实现,因为按键事件的触发取决于操作系统对按键应如何重复的想法。这意味着可能会有可变的初始和后续延迟,同时按住两个键只会导致其中一个重复。
您必须记录每个键当前是否被按下,并keydown
在键已按下时忽略事件。这是因为当自动重复发生时,许多浏览器会触发一个keydown
和一个keypress
事件,如果你自己复制关键重复,你需要抑制它。
例如:
// Keyboard input with customisable repeat (set to 0 for no key repeat)
//
function KeyboardController(keys, repeat) {
// Lookup of key codes to timer ID, or null for no repeat
//
var timers= {};
// When key is pressed and we don't already think it's pressed, call the
// key action callback and set a timer to generate another one after a delay
//
document.onkeydown= function(event) {
var key= (event || window.event).keyCode;
if (!(key in keys))
return true;
if (!(key in timers)) {
timers[key]= null;
keys[key]();
if (repeat!==0)
timers[key]= setInterval(keys[key], repeat);
}
return false;
};
// Cancel timeout and mark key as released on keyup
//
document.onkeyup= function(event) {
var key= (event || window.event).keyCode;
if (key in timers) {
if (timers[key]!==null)
clearInterval(timers[key]);
delete timers[key];
}
};
// When window is unfocused we may not get key events. To prevent this
// causing a key to 'get stuck down', cancel all held keys
//
window.onblur= function() {
for (key in timers)
if (timers[key]!==null)
clearInterval(timers[key]);
timers= {};
};
};
然后:
// Arrow key movement. Repeat key five times a second
//
KeyboardController({
37: function() { Move(-1, 0); },
38: function() { Move(0, -1); },
39: function() { Move(1, 0); },
40: function() { Move(0, 1); }
}, 200);
尽管如此,大多数基于动作的游戏都有一个固定时间的主帧循环,您可以将键向上/向下处理绑定到其中。