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 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Keyboard events on IE don't work

    Hi, I am trying to get this code to work in IE. It works on Firefox, but not on IE. Basically it blocks out scrolling the page down with space button, and registers up and down keys to run some code. The entire case statement including event registering and my code works completely fine in FF but it just won't work in IE (meaning that I can scroll down with space, and up and down with the up and down keys respectively).

    Here's the code, any help would be much appreciated. By the way I put this function in the HEAD section.

    Code:
    window.onkeydown = function(event)
        {
            // No space scrolling!
            switch (event.keyCode)
            {
                case 32: return false; break;
                case 38:
                {
                    if (rB_Script.last_item <= 1000)
                    {
                        return;
                    }
                    else
                    {
                        rB_Script.focus_on_step(rB_Script.last_item - 1);
                    }
                    return false; break;
                }
                case 40:
                {
                    if (rB_Script.last_item >= rB_Script.final_item)
                    {
                        return;
                    }
                    else if (rB_Script.last_item != -1)
                    {
                        rB_Script.focus_on_step(rB_Script.last_item + 1);
                    }
                    else
                    {
                        rB_Script.focus_on_step(1000);
                    }
                    return false; break;
                }
            }
        }
    Thank you very much!

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    First, it should be document.onkeydown, not window.onkeydown.

    Second, you can't use the identifier event as the function's argument. Doing that in IE will shadow the window level event object (which is the one you want) with an undefined object of the same name.



    Try this code instead, using evt everywhere that you have event:
    Code:
    document.onkeydown = function( evt ) {
      evt = evt || event;   // get the appropriate event object, depending on the browser
    
      // your code, using 'evt' instead of 'event'
    }


  •  

    Posting Permissions

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