Web Analytics Made Easy -
StatCounter Onkeypress - CodingForum

Announcement

Collapse
No announcement yet.

Onkeypress

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Onkeypress

    I am trying to figure out how to display an alert box when the user presses the "h" key? Can anybody help me out?

  • #2
    Sure thing...

    I dont know what your trying to do but id use onKeyDown instead of onKeyPress, but its your call.

    ok in the body tag you go like this

    Code:
    <body onKeyPress="if(window.event.keyCode==72)alert('you pressed H');" >
    I dont know how good you are with javascript but the keyCode for "H" is 72

    hope that helps
    UNDER CONSTRUCTION

    Comment


    • #3
      Originally posted by warhammerdude20 View Post
      Sure thing...

      I dont know what your trying to do but id use onKeyDown instead of onKeyPress, but its your call.

      ok in the body tag you go like this

      Code:
      <body onKeyPress="if(window.event.keyCode==72)alert('you pressed H');" >
      I dont know how good you are with javascript but the keyCode for "H" is 72

      hope that helps
      ASCII keyCode 72 is H upper case. Lower case h is keyCode 104.

      So: <body onKeyPress="if (window.event.keyCode == 72 || window.event.keyCode == 104) alert ('You pressed h or H');" >

      All the code given in this post has been tested and is intended to address the question asked.
      Unless stated otherwise it is not just a demonstration.

      Comment


      • #4
        If you want it to work in other browsers than Internet Explorer:

        <body onkeypress="var k=event.keyCode||event.charCode||event.which;if(k==72||k==104)alert('you pressed H or h');return false;">
        liorean <[[email protected]]>
        Articles: RegEx evolt wsabstract , Named Arguments
        Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
        Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

        Comment

        Working...
        X