IE e.keyCode - 如何区分&符号和上箭头? [英] IE e.keyCode - How can I differentiate between ampersand and up-arrow?
问题描述
我正在尝试修复jQuery UI小部件上的一个非常奇怪的javascript行为。
IE7(win XP),jQuery 1.2.6(是的,它是旧版本)。
I am fighting with a very strange javascript behavior on a jQuery UI widget I'm trying to fix. IE7 (win XP), jQuery 1.2.6 (yes, it's an old version).
小部件是一个组合框,可以捕获键盘事件并且箭头键有特殊行为。
The widget is a combo-box, which captures keyboard events and has special behaviors for the arrow keys.
当我尝试输入&时字符进入flexbox输入字段,我得到了奇怪的行为。
When I try to type the "&" character into the flexbox input field, I get strange behavior.
flexbox有一些代码如:
The flexbox has some code like:
//initialization
$myInputElement.keypress($.flexbox.process_key);
$.flexbox.process_key = function process_key(e) {
$.flexbox.flexboxFromInput(this).processKey(e);
return true;
};
//on the flexbox object's prototype:
...
processKey: function processKey(e) {
var mod = 0;
if (typeof (e.ctrlKey) !== 'undefined') {
if (e.ctrlKey) mod |= 1;
if (e.shiftKey) mod |= 2;
} else {
if (e.modifiers & Event.CONTROL_MASK) mod |= 1;
if (e.modifiers & Event.SHIFT_MASK) mod |= 2;
}
...
switch (e.keyCode) {
case 38: // up
this.prevResult();
break;
case 40: // down
if (this.getCtr().is(':visible')) this.nextResult();
else this.flexboxDelay(true);
break;
...etc.
}
}
...
当我介绍一个记录声明,我发现是按& (shift + 7)产生三个按键事件:
When I introduce a logging statement, what I find is that pressing "&" (shift+7) produces three keypress events:
INFO: Flexbox> processKey, keyCode=16, ctrl=false, shift=true
INFO: Flexbox> processKey, keyCode=55, ctrl=false, shift=true
INFO: Flexbox> processKey, keyCode=38, ctrl=false, shift=true
显然,keyCode 38都是up箭头键和&符号的ASCII代码??
Apparently, keyCode 38 is both the up arrow key and the ASCII code for ampersand??
当我写这篇文章时,我发现我可以将按键检测为shift + 7(keyCode 55) )将它视为&符号键,然后设置某种标志以忽略下一个按键(即38)。这似乎是一个可怕的黑客。
As I was writing this, it occurred to me that I can detect the keypress as "shift+7" (keyCode 55) to treat it as the ampersand key, then set some kind of flag to ignore the next keypress (which is the 38). This seems like a horrible hack.
是否有人有更好的方法来区分特殊字符,如& IE中的箭头键?
Does anybody have a better way to differentiate between special characters such as "&" and the arrow keys in IE?
推荐答案
这就是我最终做的事情:
This is what I ended up doing:
/*
* Hack around a wierd behavior in IE where "&%'(" have the same keyCodes
* as the arrow keys.
*/
if (keyCodeIn(e.keyCode, 55, 53, 57) && (mod & 2) && !(mod & 1)) {
this.ignoreNextArrowKey = true;
}
else if (222 === e.keyCode && !(mod & 2) && !(mod & 1)) {
this.ignoreNextArrowKey = true;
}
else if (keyCodeIn(e.keyCode, 38, 37, 39, 40) && this.ignoreNextArrowKey) {
this.ignoreNextArrowKey = false;
return;
}
//...
function keyCodeIn(keyCode) {
for(var i = 1; i < arguments.length; i++) {
if (arguments[i] === keyCode) {
return true;
}
}
return false;
}
希望这有助于某人。如果您正在重复使用此代码,则可能需要调整关键字this的使用情况,具体取决于它引用的对象(此处,它是与flexbox小部件关联的javascript对象)。
Hope this helps somebody. If you are reusing this code, you may have to adjust your usage of keyword 'this', depending on what object it refers to (here, it's a javascript object associated with the flexbox widget).
编辑:我更新了这段代码,并删除了之前存在的正则表达式测试,我发现它是错误的。
I updated this code, and removed the regular expression test that was previously there, which I discovered was buggy.
这篇关于IE e.keyCode - 如何区分&符号和上箭头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!