I have a web front-end to an AS/400 CGI application which allows the use of some of the F1-F24 keys (depending on the page) as well as page-up, page-down etc - these are passed to the underlying application which handles them appropriately. For instance, on a given page, a user could either press the F3 button or press the F3 key - both of them will set the (hidden) CmdKey variable to have a name of '_K03' and a value of 'F03'. The button handling is simple and has no problems. To handle users pressing an actual F-key on the keyboard, I have had an IE-compatible script for a long time which works perfectly:
function setCmdKeyIE() {
var cmdkeycode = "";
if (window.event.keyCode != 13 &
window.event.keyCode != 33 &
window.event.keyCode != 34 &
window.event.keyCode < 112 ) return;
window.event.keyCode = window.event.keyCode + 1000;
if (window.event.shiftKey) window.event.keyCode = window.event.keyCode + 1000;
switch(window.event.keyCode) {
case 1013: cmdkeycode = "EN"; break; /* Enter */
case 1033: cmdkeycode = "UP"; break; /* Page Up */
case 1034: cmdkeycode = "DN"; break; /* Page Down */
case 1112: cmdkeycode = "01"; break; /* F1 */
case 1113: cmdkeycode = "02"; break; /* F2 */
...(F3 thru F24 here)...
default: return; /* Anything else should be ignored */
}
window.event.cancelBubble = true;
window.event.returnValue = false;
document.forms[0].CmdKey.value = "F" + cmdkeycode;
document.forms[0].CmdKey.name = "_K" + cmdkeycode;
if (ONSUBMITFUN() == true) document.forms[0].submit();
}
This not only sets the CmdKey element correctly, but it also overrides (stops) the browser default behavior (if any) from being executed (For instance, when the user presses F3, the Search box doesn't appear).
The setCmdKeyIE() function is invoked thus:
<body onKeyDown="setCmdKeyIE();" onHelp="return false;">
I now need this to work for Firefox (and, potentially other browsers) and I'm having all sorts of trouble. I initially changed the setCmdKeyIE function (yes, I know the name should be changed once it's no longer IE-specific, but that's the least of my worries!) to get the event as a parameter (which would only be the case with FF, I thought) or to use the current behavior if it's not passed (with IE). I also added some other processing to stop FF event propagation, but it isn't working...
Here's the new non-working code - can some kind soul point out the error of my ways?
function setCmdKey(e) {
if (!e) {
var e = window.event; /* IE event-handling */
}
var wrkkeyCode = e.keyCode;
if (wrkkeyCode != 13 &
wrkkeyCode != 33 &
wrkkeyCode != 34 &
wrkkeyCode != 27 &
wrkkeyCode < 112 ) return;
wrkkeyCode = wrkkeyCode + 1000;
if (e.shiftKey) wrkkeyCode = wrkkeyCode + 1000;
var cmdkeycode = "";
switch(wrkkeyCode) {
case 1013: cmdkeycode = "EN"; break; /* Enter */
case 1033: cmdkeycode = "UP"; break; /* Page Up */
case 1034: cmdkeycode = "DN"; break; /* Page Down */
case 1112: cmdkeycode = "01"; break; /* F1 */
case 1113: cmdkeycode = "02"; break; /* F2 */
...(F3 thru F24 here)...
default: return; /* Anything else should be ignored */
}
if (e.stopPropagation) { /* FF */
e.stopPropagation();
e.preventDefault();
}
else { /* IE */
e.cancelBubble = true;
e.returnValue = false;
}
document.forms[0].CmdKey.value = "F" + cmdkeycode;
document.forms[0].CmdKey.name = "_K" + cmdkeycode;
if (ONSUBMITFUN() == true) document.forms[0].submit();
}
Do I need to return false from setCmdKeyIE with FF? Does this hold true even if this procedure returns false?
UPDATED
I've sorted this out now. Sorry for removing the context for the comments below, but hey, the previous versions are still there.
It turns out that there are two problems to be fixed in IE: first is that contrary to what I said before, putting an
onkeydown
attribute in the<body>
doesn't work. You need to attach it to the document instead. The second issue is that IE won't let you suppress magic behaviour such as the search dialog triggered by the F3 key unless you do an evil hack involving changing thekeydown
event'skeyCode
property, which is clearly a Very Wrong Thing Indeed. Be that as it may, remove theonkeydown
attribute in the<body>
and the following should do the job (now amended to work in Opera also):You should always return false from event handlers even if you have prevented default by modifying the event, see Quirks mode for more information.