2

I need the keycodes for something but I don't know what kind of keycode it is, I couldn't find it anywhere else.

 function keyDown(e) {
  if (String.fromCharCode(e.keyCode) == "%") isLeft = true;
  if (String.fromCharCode(e.keyCode) == "'") isRight = true;
}

function keyUp(e) {
  if (String.fromCharCode(e.keyCode) == "%") isLeft = false;
  if (String.fromCharCode(e.keyCode) == "'") isRight = false;



}

I figured out that % is left arrow and ' is the right arrow, could anyone tell me what kind of keycode thingy this is? or maybe how I could change it to a better one.

Thanks :)

4 Answers 4

1

You can easily find keycode values by googling for "javascript keycodes", or just use this website: keycode.info

The keycode for left arrow is 37, right arrow is 39.

In your code you converted e.keyCode to an ASCII character, which is unnecessary. Unsurprisingly, ASCII character #37 is %, #39 is '.

Just compare the keycode values directly:

function keyDown(e) {
    if (e.keyCode == 37) isLeft = true;
    if (e.keyCode == 39) isRight = true;
}

function keyUp(e) {
    if (e.keyCode == 37) isLeft = false;
    if (e.keyCode == 39) isRight = false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

e.keyCode is deprecated. e.key or e.code should be used instead, which also makes the code easier to understand. Just keep in mind some old browsers used non-standard codes, so left is usually LeftArrow and right is RightArrow, but on IE it's just Left and Right instead. Also, take a look at keyjs.dev. I will add information about these kinds of cross-browser incompatibilities soon! 🙂
1

You could use e.key, it returns value of the pressed key. So if you press left arrow key e.key would be "ArrowLeft" and if you press right arrow key e.key would be "ArrowRight".

1 Comment

Note some old browsers used non-standard codes, so those would be just 'Left' and 'Right' on IE.
1

You can lookup these codes Keycode table

or

check them interactively here: http://keycode.info/ or http://keycodes.atjayjo.com/

The function String.fromCharCode will then convert a unicode number into a character.

For cross browser compatibility you might want to check both the e.which and e.keyCode properties like below.

function(e) {
  e = e || window.event;
  var keyCode= e.which || e.keyCode;
  var charStr = String.fromCharCode(keyCode);
  alert("The keycode is: "+ keyCode + " and charCode is: " + charStr);
}

Comments

0

You don't need to get the character for those key codes, you could just compare them directly:

function keyDown(e) {
  if (e.keyCode === 37) isLeft = true;
  if (e.keyCode === 39) isRight = true;
}

function keyUp(e) {
  if (e.keyCode === 37) isLeft = false;
  if (e.keyCode === 39) isRight = false;
}

Anyway, keep in mind e.keyCode is deprecated, so e.key or e.code should be used instead, which also makes the code easier to understand. Also, you could use an object with boolean properties instead of individual booleans to make this scale better to more keys:

const pressedKeys = {};

document.onkeydown = ({ key }) => {
  pressedKeys[key] = true;
  
  console.log(Object.keys(pressedKeys).join(' + '));
};

document.onkeyup = ({ key }) => {
  delete pressedKeys[key];
  
  console.log(Object.keys(pressedKeys).join(' + '));
};

Just keep in mind some old browsers used non-standard codes, so left is usually 'LeftArrow ' and right is 'RightArrow', but on IE it's just 'Left' and 'Right' instead.

Also, if you need to check KeyboardEvent's properties values such as e.key, e.code, e.which or e.keyCode you can use https://keyjs.dev. I will add information about these kinds of cross-browser incompatibilities soon!

Key.js \ JavaScript KeyboardEvent's key codes & key identifiers

Disclaimer: I'm the author.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.