Results 1 to 2 of 2

Thread: [RESOLVED] keycode

  1. #1

    Thread Starter
    Fanatic Member merhaba's Avatar
    Join Date
    Sep 2002
    Location
    Istanbul,Bartin-Gallipoli(Gelibolu-Canakkale)
    Posts
    601

    Resolved [RESOLVED] keycode

    What is wrong with the following code..it does not work for me?

    PHP Code:
    <HTML>
    <
    HEAD>
    <
    SCRIPT language="JavaScript">
        function 
    GetKeyCode() { 
            {
            
    alert("KeyCode is " document.event.KeyCode); 
    }
        
    document.keydown=GetKeyCode(); 


    </
    SCRIPT>
    </
    HEAD
    <
    BODY onload="GetKeyCode()"
    <
    PPress the key you want to know the KeyCode 
    </BODY>
    </
    HTML

  2. #2
    Junior Member
    Join Date
    Jan 2006
    Posts
    26

    Re: keycode

    Use this script:

    <script language="JavaScript">
    document.onkeydown = checkKeycode
    function checkKeycode(e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    alert("keycode: " + keycode);
    }
    </script>

    you don't need a body onload function because the script will fire whenever a keypress is detected. One problem with your code is that when using document model functions, you do not use the parenthesis () when assigning a function to an event. So, you would use document.onkeydown = checkKeycode and NOT document.onkeydown = checkKeycode()
    Another problem is the syntax you have used to capture the keycode is not correct. Both Explorer and Firefox capture differently, as shown in the code above in that there are two captures within if-then logic.

Posting Permissions

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



Click Here to Expand Forum to Full Width