How to know key values in Javascript?

Asked

Viewed 278 times

3

I’m developing a basic game in Javascript, and the player has some actions, already implemented the ActionListener, but I’m having trouble finding the key values to use in ActionListener. I was taking 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?

  • Hello, you can use this code generator + guide to call in your app. https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

  • A quick way to discover the key code would be by using this website.

  • Could you add your code that you have already done? It would help us better understand your problem!

2 answers

7


You can do it as follows with pure javascript :

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

See working, example.

Source

-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.