Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript keycode =/= charcode

I'm binding on the keydown event with jQuery to allow some specific characters in a textbox. I'm trying to make my code cross browser and platform compliant. My main issue is that I can't find a way to map the received key-code to a valid char-code, especially for key code coming from the numeric pad (decimal separator, is it a comma or a period, etc.).

like image 402
Fabian Vilers Avatar asked Dec 11 '09 12:12

Fabian Vilers


1 Answers

That is correct. There is no such mapping you can do; a keydown may not even correspond to a character being inserted. Or Caps Lock might change the meaning of the keypress, and you can't sniff for that. And various other wrinkles.

If you want to know the character code your only option is to use the keypress event.

like image 163
bobince Avatar answered Sep 28 '22 18:09

bobince