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: event.keyCode

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    event.keyCode

    I am simply trying to capture and identify a keystroke, and the following gives the error message "object requried" in IE and "event not defined" in Netscape.

    The problem is with var key=event.keyCode;

    The keystroke is taking me to JumpKey() just fine.

    Thanks.

    <body onKeyPress='JumpKey()'>

    // Function to accept keystroke
    function JumpKey(){
    var key=event.keyCode;
    alert(key);
    }

  • #2
    New to the CF scene
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Hey, I dont see why this wouldnt work, I tried it out, and works fine.
    Here is my exact code just in case.

    <body onKeyPress='JumpKey()'>

    </body>
    <script language="javascript">
    function JumpKey(){
    var key=event.keyCode;
    alert(key);
    }
    </script>

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks, got it working.

    Having you tell me the code was good got me thinking a little harder. I'm using frames:

    var key=parent.image.event.keyCode;

    Fixed it.

    Thanks, Peter

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    here's a complete and crossbrowser code to capture the keycode (what you are using is IE only) on document or, at will, on a form's text field element, if needed:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    title>Captura key code</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    //Genuine code by Corneliu Lucian 'KOR' Rusu  -- mailto:corneliulucian@apropo.ro
    function capturekey(e){//on text field
    var key=(typeof event!='undefined')?window.event.keyCode:e.keyCode;
    alert('keycode : '+key);
    }
    //on document
    if(navigator.appName!= "Mozilla"){document.onkeypress=capturekey}
    else{
    document.addEventListener("keyup",capturekey,true)}
    //END on document
    </script>
    </head>
    <body>
    <input type="text" onkeypress="capturekey(event)">
    </body> 
    </html> 
    Last edited by Kor; 08-12-2005 at 05:26 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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