问题
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 Firefox, I thought) or to use the current behavior if it's not passed (with IE). I also added some other processing to stop Firefox 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 Firefox? Does this hold true even if this procedure returns false?
回答1:
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 the keydown
event's keyCode
property, which is clearly a Very Wrong Thing Indeed. Be that as it may, remove the onkeydown
attribute in the <body>
and the following should do the job (now amended to work in Opera also):
var keyCodeMap = {
"1013": "EN",
"1033": "UP",
"1034": "DN",
"1112": "01",
"1113": "02",
"1114": "03"
// ...(F4 thru F24 here)...
};
var suppressKeypress = false;
function setCmdKey(e) {
e = e || window.event;
var wrkkeyCode = e.keyCode;
if (wrkkeyCode != 13 &&
wrkkeyCode != 33 &&
wrkkeyCode != 34 &&
wrkkeyCode != 27 &&
wrkkeyCode < 112) return;
wrkkeyCode += 1000;
if (e.shiftKey) wrkkeyCode += 1000;
var cmdkeycode = keyCodeMap[wrkkeyCode];
if (!cmdkeycode) return; /* Anything else should be ignored */
var input = document.forms[0].elements["CmdKey"];
input.value = "F" + cmdkeycode;
input.name = "_K" + cmdkeycode;
try {
// Prevent default action in IE by bad hacky means
e.keyCode = 0;
} catch (ex) {
// Other browsers do not allow setting the keyCode
}
suppressKeypress = true;
if (ONSUBMITFUN()) document.forms[0].submit();
return false;
}
document.onkeydown = setCmdKey;
document.onkeypress = function() {
if (suppressKeypress) {
return false;
}
};
回答2:
You should always return false from event handlers even if you have prevented default by modifying the event, see Quirks mode for more information.
来源:https://stackoverflow.com/questions/3286174/capturing-onkeydown-in-javascript