The following code works for capturing key presses on IE, Netscape, Opera 7,and somewhat with Opera 6. HTML: <html> <script language="JavaScript"> function keyHandler(e) { var pressedKey; if (document.all) { e = window.event; } if (document.layers) { pressedKey = e.which; } if (document.all) { pressedKey = e.keyCode; } pressedCharacter = String.fromCharCode(pressedKey); alert(' Character = ' + pressedCharacter + ' [Decimal value = ' + pressedKey + ']'); } document.onkeypress = keyHandler; </script> <body>Press any key.
Oh! That can be fixed with changing line 7 to this Code: if (document.layers || e.which) { pressedKey = e.which; }
Everyone, Please keep the discussion related to the article here and other discussions in the respective threads or create a new one.
thanks for the code but it had a small bug in it which was when you press 2 keys very very fast it will capture 1 of them and the other just be ignored, I am personally using it to disable the Fx keys and the arrow keys etc... letting the user use only the alphanumeric keys... here is what I did Code: <script language="JavaScript"> <!-- function keyHandler(e) { var pressedKey; if (document.all) { e = window.event; pressedKey = e.keyCode; } if (document.layers || e.which) { pressedKey = e.which; } if (!pressedKey) { return false; } } document.onkeypress = keyHandler; --> </script> I added the comment tags cause in IE 5 it sometimes prints out the code so using those will make it think it's a comment so it will just be ignored :P tnx alot for the code you gave out here, you really helped me and also thnx to pradeep for telling us how to make it work with FF too hope what I did helps you as much as what you did helped me
can i perform reverse operation using KeyCode. i am giving Some Keycode Separated with comma (','). can you give me result of it. and how to get this result. 72,69,76,76,79,8,8,8 mine expected result is "hi"