Capture Key Press

Discussion in 'Web Design, HTML And CSS' started by pradeep, Jan 29, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Does it work with FF
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Oh! That can be fixed with changing line 7 to this

    Code:
    if (document.layers || e.which) { pressedKey = e.which; }
     
  4. kanchan

    kanchan New Member

    Joined:
    Feb 4, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    e-content developer(multimedia and animation)
    Location:
    Aurangabad
    hi pradeep.
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Hi, Do i know you?
     
  6. kanchan

    kanchan New Member

    Joined:
    Feb 4, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    e-content developer(multimedia and animation)
    Location:
    Aurangabad
    no you dont know me
    I am from aurangabad.
     
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Can I help you out in some way?
     
  8. rahul bakshi

    rahul bakshi New Member

    Joined:
    May 31, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi i m rahul.Your promming excellent for everyone.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Everyone, Please keep the discussion related to the article here and other discussions in the respective threads or create a new one.
     
  10. ClayC

    ClayC New Member

    Joined:
    Mar 27, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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 :D
     
    Last edited by a moderator: Mar 31, 2009
  11. mihirlak

    mihirlak New Member

    Joined:
    Aug 16, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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"
     
  12. mihirlak

    mihirlak New Member

    Joined:
    Aug 16, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    mine expected result is "he"
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice