Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Onkeypress

  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote 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');" >

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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 <[lio@wg]>
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •