How to create a JavaScript code for multiple keys pressed at once?

JavascriptObject Oriented ProgrammingFront End Technology

Use the keydown event in JavaScript to get to know which keys are pressed at once. The following is the script −

Example

var log = $('#log')[0],
   keyPressed = [];

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

$(document.body).keyup(function (evt) {
   var li = keyPressed [evt.keyCode];
   if (!li) {
      li = log.appendChild(document.createElement('li'));
   }
   $(li).text('Key Up: ' + evt.keyCode);
   $(li).addClass('key-up');
});
raja
Published on 27-Feb-2018 11:03:16
Advertisements