JavaScript两个键同时按下

时间:2013-07-06 15:48:08

标签: javascript key onkeydown pressed

当我按下键盘上的两个键时,如何同时获得两个代码?

在其中,我只获得一个keyCode ......

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

1 个答案:

答案 0 :(得分:0)

看这里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');
});