Possible Duplicate:
Javascript KeyCode vs CharCode = Utter Confusion
What is the difference between the return value of String.charCodeAt(index) and JavaScript key codes? And is there a way to translate between the two?
For example:
console.log('.'.charCodeAt(0)); // prints 46 on Mac/Chrome
But the keyCode for ‘.’ is 190.
string.charCodeAt(index) is designed to return you the ascii value for the character of a string at a specified position.
keyCode is the browser implementation of what the value for the keyboard key that was pressed. unfortunately, it’s not standardized across all browsers either.
Edit:
String.fromCharCode(unicodeValue);This will convert a unicode value such as keyCode into an ascii value. Just be mindful that not all keyCode values are going to convert to an appropriate ascii value (since there isn’t one) such key presses like { delete, up, down, left, right, home, insert, page up, page down }
Example: pressing the Delete key returns a keyCode of 46, if you do
alert(String.fromCharCode(46));you will see it output a period since the 46 is the ascii value for a period.