删除javascript中的按键延迟 [英] remove key press delay in javascript

查看:122
本文介绍了删除javascript中的按键延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:
我正在尝试编写一个javascript游戏,字符由箭头键控制。

问题是当一个人保持密钥按下,第一个按键和重复的kepress之间有一个很短的延迟。

此外,当按下向右箭头键并保持按下时,然后按向上箭头键字符不会移动到右上角,但是停止向正确的方向移动,并开始向上移动。

这是我使用的代码:

 < body onLoad =Load()onKeyDown =Pressed(event)> 


 
函数按(e){
cxc = e。关键代码;
if(cxc == 37)
Move(-1,0);
if(cxc == 38)
Move(0,-1);
if(cxc == 39)
Move(1,0);
if(cxc == 40)
Move(0,1);
}

有人有想法吗?

解决方案

如果您想要以可控制的方式重复键盘,您将不得不自行实施,因为按键操作系统的关键点如何重复的想法会触发按键事件。这意味着可能存在可变的初始和后续延迟,同时按住两个键将导致只有其中一个重复。



您将不得不保留记录当前键是否按下,忽略 keydown 事件。这是因为许多浏览器会在autorepeat发生时触发 keydown 以及 keypress 事件,如果你'重新复制键,重复你自己,你需要压制。



例如:

 code> //键盘输入与可自定义的重复(设置为0,无键重复)
//
函数KeyboardController(keys,repeat){
//查找关键代码计时器ID,否则为无重复
//
var timers = {};

//当按下键并且我们还没有认为按下时,调用
//键操作回调,并设置一个计时器,在延迟后生成另一个
//
document.onkeydown = function(event){
var key =(event || window.event).keyCode;
if(!(key in keys))
return true;
if(!(key in timers)){
timers [key] = null;
键[key]();
if(repeat!== 0)
timers [key] = setInterval(keys [key],repeat);
}
返回false;
};

//取消超时和标记键在keyup上释放
//
document.onkeyup = function(event){
var key =(event || window .event).keyCode;
if(key in timers){
if(timers [key]!== null)
clearInterval(timers [key]);
删除计时器[key];
}
};

//当窗口未聚焦时,我们可能无法获取关键事件。为了防止这个
//导致一个键卡住,取消所有保持的键
//
window.onblur = function(){
for(键入计时器)
if(timers [key]!== null)
clearInterval(timers [key]);
timers = {};
};
};

然后:

 code> //箭头键移动。一键重复五次
//
KeyboardController({
37:function(){Move(-1,0);},
38:function(){Move (0,-1);},
39:function(){Move(1,0);},
40:function(){Move(0,1);}
},200);

尽管大多数基于动作的游戏都有固定时间的主框架循环,钥匙上/下处理。


I have the following problem: I'm trying to write a javascript game, and the character is being controlled by the arrow keys.
The problem is, when one keeps the key pressed, there is a short delay between firing the first keypress and the repeated kepress.
Also, when one presses the "right arrow key" and keeps it pressed, and then presses the "up arrow key" the character doesn't move to the top right corner, but stops the moving in the right direction and starts moving up.
This is the code I'm using:

<body onLoad="Load()" onKeyDown="Pressed(event)">

function Pressed(e) { 
        cxc = e.keyCode;
        if(cxc == 37)
            Move(-1,0);
        if(cxc == 38)
            Move(0,-1);
        if(cxc == 39)
            Move(1,0);
        if(cxc == 40)
            Move(0,1);
    }

does anybody have an idea??

解决方案

If you want key repeat in a controllable fashion, you will have to implement it yourself, as keypress events are fired dependent on the OS's idea of how keys should repeat. That means there may be variable initial and following delays, and holding down two keys at once will cause only one of them to repeat.

You will have to keep a record of whether each key is currently pressed, and ignore keydown events when the key is already down. This is because many browsers will fire a keydown as well as a keypress event when an autorepeat occurs, and if you're reproducing key repeat yourself you'll need to suppress that.

For example:

// 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= {};
    };
};

then:

// 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);

Although, most action-based games have a fixed-time main frame loop, which you can tie the key up/down handling into.

这篇关于删除javascript中的按键延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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