Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Tutorials (http://www.go4expert.com/articles/web-design/)
-   -   Capture Key Press (http://www.go4expert.com/articles/capture-key-press-t2733/)

pradeep 29Jan2007 15:36

Capture Key Press
The following code works for capturing key presses on IE, Netscape, Opera 7,and somewhat with Opera 6.

HTML Code:

 <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;
 <body>Press any key.

shabbir 29Jan2007 17:17

Re: Capture Key Press
Does it work with FF

pradeep 29Jan2007 17:42

Re: Capture Key Press
Oh! That can be fixed with changing line 7 to this

Code: javascript

if (document.layers || e.which) { pressedKey = e.which; }

kanchan 6Feb2007 22:19

Re: Capture Key Press
hi pradeep.

pradeep 7Feb2007 10:06

Re: Capture Key Press
Hi, Do i know you?

kanchan 8Feb2007 22:13

Re: Capture Key Press
no you dont know me
I am from aurangabad.

pradeep 9Feb2007 13:47

Re: Capture Key Press
Can I help you out in some way?

rahul bakshi 23Jun2007 11:14

Re: Capture Key Press
Hi i m rahul.Your promming excellent for everyone.

shabbir 23Jun2007 14:55

Re: Capture Key Press
Everyone, Please keep the discussion related to the article here and other discussions in the respective threads or create a new one.

ClayC 27Mar2009 20:19

Re: Capture Key Press
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

<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;

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 :D

mihirlak 16Aug2011 17:13

Re: Capture Key Press
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.


mine expected result is

mihirlak 16Aug2011 17:15

Re: Capture Key Press
mine expected result is "he"

All times are GMT +5.5. The time now is 05:21.