How to know key values in Javascript?


I'm developing a basic game in Javascript , and the player has some actions, I've already implemented ActionListener , but I'm having difficulty finding key values to use in ActionListener . I was picking up the values of each key by trial and error, but it is very laborious, is it possible to know the value of all keys more easily? If possible, how?

asked by anonymous 22.02.2017 / 12:33

2 answers


You can do the following with pure javascript:

document.addEventListener("keydown", function(event)
{   console.log(event);
    document.body.innerHTML = "\n<b>keyCode:</b> " + event.keyCode;

See working, example .


22.02.2017 / 13:03

There is an excellent jquery plugin for this: link

22.02.2017 / 12:49