如何在按下双键时固定斜角

时间:2019-03-31 14:47:23

标签: javascript arrow-keys

我实际上固定了按下双键之间的延迟,但是现在我想只通过top = top和left = left来固定运动。当我使用向上+向左的双箭头时,它会倾斜并且我不想要它。

function keyboardController(keyset){
    var timers = {};

    document.onkeydown= function(event) {
        var key= (event || window.event).keyCode;
        if (!(key in keyset))
            return true;
        if (!(key in timers)) {
            timers[key]= null;
            keyset[key].callback();
            if (keyset[key].interval !== 0)
                timers[key]= setInterval(keyset[key].callback, keyset[key].interval);
        }
        return false;
    };

    document.onkeyup= function(event) {
        var key= (event || window.event).keyCode;
        if (key in timers) {
            if (timers[key]!==null)
                clearInterval(timers[key]);
            delete timers[key];

        }
    };
}

function triggerKeyboardController(){
    keyboardController({
        39: {interval:10, callback:function(){ }},
        37: {interval:10, callback:function(){ }},
        38: {interval:10, callback:function(){ }},
        40: {interval:10, callback:function(){ }}
    }, 20);
}

triggerKeyboardController();

0 个答案:

没有答案