String.fromCharCode issue for special characters in javascript
问题 Below code accepts a character and prints character using keycode(String.fromCharCode). Issue arises when you enter special character like . or ? . function logChar(e){ var keyCode = e.which || e.keyCode; var char = String.fromCharCode(keyCode); console.log(char) } <input type="text" onkeydown="logChar(event)" /> Can someone explain why is it returning different value and how to get same value? Thanks in advance! 回答1: That's because keyCode and charCode are different. String.fromCharCode