如何在Chrome和IE中跟踪箭头键? [英] how can i track arrow keys in Chrome and IE?
问题描述
我使用foloowing代码跟踪关键事件
Im using foloowing code to track key events
oEvent=window.event || oEvent;
iKeyCode=oEvent.keyCode || oEvent.which;alert(iKeyCode);
它在Firefox中给我提醒,但不能在IE和chrome中。它给我所有其他键盘字符,但不是esc键和箭头键。
its giving me alerts in firefox but not in IE and chrome. Its giving me all the other keyborad characters but not esc key and arrow keys.
如何在chrome和IE中使用javascript检测esc键和箭头键? p>
How can i detect esc key and arrow keys in chrome and IE using javascript??
推荐答案
你真的不需要JQuery,虽然它会使您的代码更短。
You don't really need JQuery, though it does make your code shorter.
您将不得不使用keyDown事件,keyPress将不能在旧版本的IE中用于箭头键。
You will have to use the keyDown event, keyPress will not work in old versions of IE for the arrow keys.
这里有一个完整的教程,您可以使用,看到示例,箭头键靠近页面底部:
http:/ /www.cryer.co.uk/resources/javascript/script20_respond_to_keypress.htm
There is a full tutorial here that you can use, see the example with arrow keys close to the bottom of the page: http://www.cryer.co.uk/resources/javascript/script20_respond_to_keypress.htm
这是我使用的一些代码,有点简化,因为我不得不处理重复缓冲的按键:
Here's some code I used, a bit simplified since I had to handle repeated keypresses with buffering:
document.onkeydown = function(event) {
if (!event)
event = window.event;
var code = event.keyCode;
if (event.charCode && code == 0)
code = event.charCode;
switch(code) {
case 37:
// Key left.
break;
case 38:
// Key up.
break;
case 39:
// Key right.
break;
case 40:
// Key down.
break;
}
event.preventDefault();
};
这篇关于如何在Chrome和IE中跟踪箭头键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!