在JavaScript中捕获onkeydown [英] Capturing onkeydown in Javascript

查看:151
本文介绍了在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;打破; / * Enter * /
case 1033:cmdkeycode =UP;打破; / * Page Up * /
case 1034:cmdkeycode =DN;打破; / * Page Down * /
case 1112:cmdkeycode =01;打破; / * F1 * /
case 1113:cmdkeycode =02;打破; / * F2 * /
...(F3到F24 here)...
默认值:return; / *任何其他都应该忽略* /
}
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特定的,但这是我的最小的忧虑!)得到事件作为参数(这将只有在FF的情况下,我想)或使用当前的行为,如果它没有传递(与IE)。我还添加了一些其他处理来停止FF事件传播,但它不工作...



这是新的非工作代码 - 可以有点灵魂指出

  function setCmdKey(e){
if(!e){
var e = window.event; / * IE事件处理* /
}
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;打破; / * Enter * /
case 1033:cmdkeycode =UP;打破; / * Page Up * /
case 1034:cmdkeycode =DN;打破; / * Page Down * /
case 1112:cmdkeycode =01;打破; / * F1 * /
case 1113:cmdkeycode =02;打破; / * F2 * /
...(F3到F24 here)...
默认值:return; / *任何其他都应该忽略* /
}
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();
}

我需要使用FF从setCmdKeyIE返回false吗?即使此过程返回false,这是否成立?

解决方案

UPDATED
$ b

我现在排序了。对不起,删除下面的评论的上下文,但嘿,以前的版本仍然存在。



事实证明,有两个问题要在IE中修复:第一与之前所说的相反,在< body> 中的 onkeydown 属性不起作用。您需要将其附加到文档。第二个问题是IE不会让你压制魔法行为,如搜索对话框由F3键触发,除非你做一个邪恶的黑客涉及改变 keydown 事件的 keyCode 属性,这显然是一个很错事。尽管如此,请删除< body> 中的 onkeydown 属性,然后执行作业现在修改为在Opera中工作):

  var keyCodeMap = {
1013:EN,
1033:UP,
1034:DN,
1112:01,
1113:02 b1114:03
// ...(F4到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; / *任何其他应该忽略* /

var input = document.forms [0] .elements [CmdKey];
input.value =F+ cmdkeycode;
input.name =_K+ cmdkeycode;

try {
//在IE中阻止默认操作bad hacky意味着
e.keyCode = 0;
} catch(ex){
//其他浏览器不允许设置keyCode
}
suppressKeypress = true;

if(ONSUBMITFUN())document.forms [0] .submit();
return false;
}

document.onkeydown = setCmdKey;
document.onkeypress = function(){
if(suppressKeypress){
return 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 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 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;
    }
};

这篇关于在JavaScript中捕获onkeydown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆