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();
}
这不仅正确设置了 CmdKey 元素,而且还覆盖(停止)执行浏览器默认行为(如果有)(例如,当用户按 F3 时,不会出现搜索框)。
The setCmdKeyIE()
函数被这样调用:
<body onKeyDown="setCmdKeyIE();" onHelp="return false;">
我现在需要它适用于 Firefox(以及可能的其他浏览器),但我遇到了各种各样的麻烦。我最初更改了 setCmdKeyIE 函数(是的,我知道一旦它不再特定于 IE,就应该更改名称,但这是我最不担心的!)以获取事件作为参数(这只适用于 Firefox,我想)或者如果没有通过(使用 IE)则使用当前行为。我还添加了一些其他处理来停止 Firefox 事件传播,但它不起作用......
这是新的无效代码 - 有好心人可以指出我的方法的错误吗?
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();
}
我需要使用 Firefox 从 setCmdKeyIE 返回 false 吗?即使此过程返回 false,这也成立吗?