JavaScript Two key pressed at the same time

删除回忆录丶 提交于 2020-01-06 14:07:34

问题


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:


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


来源:https://stackoverflow.com/questions/17504496/javascript-two-key-pressed-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!