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 to the CF scene
    Join Date
    Nov 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer temporarily disable onkeyup during event

    Hi.

    This would appear to be easy, and I want to make sure I am doing it correctly. I allow the user to press the spacebar to perform an action in addition to a button. When they press the spacebar, I want to disable the onKeyUp event for document.body so the user cannot machine gun the spacebar and cause stuff to happen I don't want to. What I'm doing is setting document.body.onkeyup = null when the spacebar press is captured and reenabling it after the action is complete. Here's my code:

    <SCRIPT LANGUAGE="JavaScript">
    function captureFKey()
    {
    document.body.onkeyup = null;

    var keycode;

    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    /*
    * Spacebar = Print Label
    */
    if (keycode == 32)
    {
    printLabel();
    return false;
    }
    else
    return true;
    }
    function printLabel()
    {
    document.getElementById("cmdPrintLabel").disabled = true;
    document.Form1.submit();

    document.body.onkeyup = "return captureFKey()";
    }
    </SCRIPT>
    <body onkeyup="return captureFKey()">

    Does this seem to be the correct way to do this? If there are better ways, please let me know.

    Thanks.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, you're not doing it right. The basic problem is that you clear the event no matter what key is pressed so pressing any key except the spacebar on the keyboard before pressing the spacebar will disable the added functionality you gave the spacebar.

    Also restoring the function should be done like this:

    Code:
    document.body.onkeyup = captureFKey;
    Check out the Forum Search. It's the short path to getting great results from this forum.


  •  

    Posting Permissions

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