在 Javascript 中捕获 onkeydown
我有一个 AS/400 CGI 应用程序的 Web 前端,它允许使用一些 F1-F24 键(取决于页面)以及页面-向上、向下翻页等 - 这些被传递到适当处理它们的底层应用程序。例如,在给定页面上,用户可以按 F3 按钮或按 F3 键 - 两者都会将(隐藏)CmdKey 变量设置为具有名称为“_K03”,值为“F03”。按钮操作简单,没有任何问题。为了处理用户按下键盘上的实际 F 键,我长期以来一直有一个 IE 兼容的脚本,该脚本运行良好:
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 时,搜索框不会出现)。
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,这也成立吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使您通过修改事件阻止了默认设置,您也应该始终从事件处理程序返回 false,请参阅 Quirks 模式 了解更多信息。
You should always return false from event handlers even if you have prevented default by modifying the event, see Quirks mode for more information.
已更新
我现在已经解决了这个问题。抱歉删除了下面评论的上下文,但是嘿,以前的版本仍然存在。
事实证明,IE 中有两个问题需要修复:第一,与我之前所说的相反,在
中放置一个
onkeydown
属性并不适用。不工作。您需要将其附加到文档中。第二个问题是,IE 不会让您抑制魔法行为,例如由 F3 键触发的搜索对话框,除非您进行了涉及更改keydown
事件的keyCode
的邪恶黑客行为。财产,这显然是一件非常错误的事情。尽管如此,删除中的
onkeydown
属性,下面的代码应该可以完成这项工作(现在已修改为也可以在 Opera 中使用):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):