View Full Version : temporarily disable onkeyup during event

11-01-2004, 08:40 PM

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:

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)
return false;
return true;
function printLabel()
document.getElementById("cmdPrintLabel").disabled = true;

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

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


Roy Sinclair
11-01-2004, 10:21 PM
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:

document.body.onkeyup = captureFKey;