JavaScript Two key pressed at the same time

2019-08-18 03:03发布

How to get at the same time two codes when i have pressed two keys on the keyboard?

In it, I only get one keyCode...

document.onkeydown = function(e)
{  var key = e.keyCode ? e.keyCode : e.which;
   text.innerHTML += key; };            

1条回答
Explosion°爆炸
2楼-- · 2019-08-18 03:33

look here JavaScript multiple keys pressed at once

http://jsfiddle.net/vor0nwe/mkHsU/

zsvar log = $('#log')[0],
    pressedKeys = [];

$(document.body).keydown(function (evt) {
    var li = pressedKeys[evt.keyCode];
    if (!li) {
        li = log.appendChild(document.createElement('li'));
        pressedKeys[evt.keyCode] = li;
    }
    $(li).text('Down: ' + evt.keyCode);
    $(li).removeClass('key-up');
});

$(document.body).keyup(function (evt) {
    var li = pressedKeys[evt.keyCode];
    if (!li) {
       li = log.appendChild(document.createElement('li'));
    }
    $(li).text('Up: ' + evt.keyCode);
    $(li).addClass('key-up');
});
查看更多
登录 后发表回答