JavaScript onkeydown / onkeyup事件未触发

时间:2018-07-07 03:23:22

标签: javascript

我正在尝试为agar.io制作一个Tampermonkey脚本,该脚本可让您为某些动作创建键绑定,但在游戏中按键无济于事,我在Google上看过但似乎无济于事。

document.getElementById("canvas").onkeydown = function(event)
{
    var key = event.keyCode || event.which || event;
    var binds = JSON.parse(localStorage.getItem("agarKeybinds"));

    for (var i = 0; i < binds.length; i++)
    {
        if (key == binds[i].charCodeAt(0))
        {
            switch (i)
            {
                case 0:
                    rapidEject = setInterval(core.eject, 50);
                    break;

                case 1:
                    for (var k = 0; k < 4; k++)
                    {
                        core.split();
                    }
                    break;
            }
        }
    }
}

document.getElementById("canvas").onkeyup = function(event)
{
    var key = event.keyCode || event.which || event;
    var binds = JSON.parse(localStorage.getItem("agarKeybinds"));

    for (var i = 0; i < binds.length; i++)
    {
        if (key == binds[i].charCodeAt(0))
        {
            switch (i)
            {
                case 0:
                    clearInterval(rapidEject);
                    break;
            }
        }
    }
}

0 个答案:

没有答案