2

Why does code:

','.charCodeAt(0);

generates 44 even though charcode for comma is 188 (both by this table and when getting event.keyCode in keyup handler).

Drecker
  • 1,215
  • 10
  • 24
  • 4
    `charCodeAt()` returns the ascii value of the character. `44` is the ascii code of a comma. See http://www.asciitable.com/ for more. – Tro Sep 08 '15 at 16:18
  • 4
    Characters and keys are two different things. That's why they don't necessarily have the same code. – gre_gor Sep 08 '15 at 16:20
  • I understand the confusion, especially when SO has questions like http://stackoverflow.com/questions/1430989/how-to-convert-char-to-keycode where the accepted answer confuses the two terms and other answers contain outdated information. These should ideally be cleaned up. – Jan Sep 08 '15 at 16:31
  • @gre_gor please make this an answer. – Pavlo Sep 08 '15 at 16:48
  • I'm feeling a bit silly right now. Thanks for answers (if any of you would make actual answer out of your comments I would gladly officially accept it). Seems like I would have to "convert" keycodes to chars some other way – Drecker Sep 08 '15 at 16:55

1 Answers1

2

Characters and keys are different things.

charCodeAt returns the numeric Unicode value of the character.
keyCode from the KeyboardEvent represents a key on a keyboard.

Since there is no 1:1 mapping between characters and keys, they don't necessarily use the same values.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • And if you absoutely *have* to map keycodes to charcodes you could do so through an array. You would have to write that implementation yourself though, it's not built-in. – Jan Sep 09 '15 at 00:46