...

View Full Version : temporarily disable onkeyup during event



slimflem
11-01-2004, 08:40 PM
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.

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;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum