0

I've been looking around and it seems that String.fromCharCode() is the method I was looking for, however, when trying to convert a keyCode using String.fromCharCode() the results are completely off

For example:

let foo = 40;
console.log(String.fromCharCode(foo))
//Output "("

When at https://keycode.info 40 is ArrowDown, and also is ArrowDown when logging event.key as such:

function logKey(event) {
    console.log(event.key);
    console.log("Key: " + event.keyCode)
}
document.addEventListener("keydown", logKey)

What is happening here?

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    Does this answer your question? [String.fromCharCode issue for special characters in javascript](https://stackoverflow.com/questions/34768354/string-fromcharcode-issue-for-special-characters-in-javascript) – Mohamed Sa'ed Dec 23 '20 at 14:04
  • [Only if you've got the "key code" from a keypress event. In keyup and keydown events, the keyCode property has nothing to do with characters.](https://stackoverflow.com/questions/3977792/how-to-convert-keycode-to-character-using-javascript#comment4255671_3977802) – 0stone0 Dec 23 '20 at 14:06
  • Don't use `event.keyCode`. It is deprecated. It gives information about the keyboard "scan code", which does not necessarily have a relation with the key's name. – trincot Dec 23 '20 at 14:07
  • Ok that makes sense now, so there isn't any other method to convert back into a `key`? – Harry Landesburg Dec 23 '20 at 14:13

0 Answers0