I would like to find out what character key is pressed in a cross-browser compatible way in pure Javascript.
"Clear" JavaScript:
function myKeyPress(e){ var keynum; if(window.event) { // IE keynum = e.keyCode; } else if(e.which){ // Netscape/Firefox/Opera keynum = e.which; } alert(String.fromCharCode(keynum)); }
<input type="text" onkeypress="return myKeyPress(event)" />
1.4m articles
1.4m replys
5 comments
57.0k users