我如何处理箭头键和 < Javascript 函数中的(大于)?哪个事件和哪个代码(charCode 或 keyCode)?

发布于 2024-09-08 10:03:15 字数 250 浏览 5 评论 0 原文

如何在 Javascript 函数中处理 ArrowKeys 和 <(大于)?哪个事件和哪个代码(charCode 或 keyCode)?

我很困惑如何做到这一点。我已经非常仔细地阅读了这个链接, 事件和 keyCode+charCode,但我找不到适合我的场景的任何解决方案。

How can I handle ArrowKeys and < (Greater Than) in a Javascript function? Which event and which code (charCode Or keyCode)?

I am very confused how to do this. I have read this link very carefully,
Events and keyCode+charCode, but I could not find any solution for my scenario.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-09-15 10:03:16

使用 event.keyCode 就足够了。您只需要考虑与获取关键事件有关的浏览器兼容性问题。

这是一个捕获箭头键的基本启动示例,复制“粘贴”然后运行它:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>SO question 3181648</title>
        <script>
            document.onkeydown = function(e) {
                e = e || event; // "real browsers" || IE6/7.
                switch (e.keyCode) {
                    case 37: alert('left'); break;
                    case 38: alert('up'); break;
                    case 39: alert('right'); break;
                    case 40: alert('down'); break;
                }
            }
        </script>
    </head>
    <body>
       <p>Press one of the arrow keys.</p> 
    </body>
</html>

请注意,最好附加事件 这样或者使用 jQuery

要捕获 < 等按下的字符,请查看 Tim 的回答

Using event.keyCode is sufficient. You only need to browser compatibility issues with regard to obtaining the key event into account.

Here's a basic kickoff example which captures arrow keys, copy'n'paste'n'run it:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>SO question 3181648</title>
        <script>
            document.onkeydown = function(e) {
                e = e || event; // "real browsers" || IE6/7.
                switch (e.keyCode) {
                    case 37: alert('left'); break;
                    case 38: alert('up'); break;
                    case 39: alert('right'); break;
                    case 40: alert('down'); break;
                }
            }
        </script>
    </head>
    <body>
       <p>Press one of the arrow keys.</p> 
    </body>
</html>

Note that attaching events is better to be done this way or using jQuery.

For capturing the pressed characters like <, have a look at Tim's answer.

把人绕傻吧 2024-09-15 10:03:16

当检测非文本输入键(例如箭头键)时,使用 keydown 事件是正确的方法。要检测键入的字符(例如 <),使用 keypress 事件是唯一安全的方法。如果您改为使用 keydown 事件及其 keyCode 属性,则不能保证这适用于与您自己的键盘和浏览器类型不同的键盘和浏览器类型。

如果您确实想了解 JavaScript 键处理,我推荐以下页面: http://unixpapa.com /js/key.html

这是满足您要求的示例:

document.onkeydown = function(evt) {
    evt = evt || window.event;
    switch (evt.keyCode) {
        case 37: alert("left"); break;
        case 38: alert("up"); break;
        case 39: alert("right"); break;
        case 40: alert("down"); break;
    }
};

document.onkeypress = function(evt) {
    evt = evt || window.event;
    var charCode = evt.which || evt.keyCode;
    var charStr = String.fromCharCode(charCode);
    if (charStr == "<") {
        alert("<");
    }
};

When detecting a non-text-input key such as an arrow key, using the keydown event is the correct approach. For detecting a typed character such as <, using the keypress event is the only safe approach. If you instead use the keydown event and its keyCode property, this is not guaranteed to work on types of keyboard and browsers different to your own.

If you really want to learn about JavaScript key handling, I recommend the following page: http://unixpapa.com/js/key.html

Here's an example for your requirements:

document.onkeydown = function(evt) {
    evt = evt || window.event;
    switch (evt.keyCode) {
        case 37: alert("left"); break;
        case 38: alert("up"); break;
        case 39: alert("right"); break;
        case 40: alert("down"); break;
    }
};

document.onkeypress = function(evt) {
    evt = evt || window.event;
    var charCode = evt.which || evt.keyCode;
    var charStr = String.fromCharCode(charCode);
    if (charStr == "<") {
        alert("<");
    }
};
榕城若虚 2024-09-15 10:03:16
<script type="text/javascript">
var Keys = {
      BACKSPACE: 8,  TAB: 9,        ENTER: 13,     SHIFT: 16,
      CTRL: 17,      ALT: 18,       PAUSE: 19,     CAPS: 20,
      ESC: 27,       PAGEUP: 33,    PAGEDN: 34,    END: 35,
      HOME: 36,      LEFT: 37,      UP: 38,        RIGHT: 39,
      DOWN: 40,      INSERT: 45,    DELETE: 46,       
      n0: 48, n1: 49, n2: 50, n3: 51, n4: 52,
      n5: 53, n6: 54, n7: 55, n8: 56, n9: 57,
      A:65, B:66, C:67, D:68, E:68, F:70, G:71, H:72, I:73, J:74, K:75,
      L:76, M:77, N:78, O:79, P:80, Q:81, R:82, S:83, T:84, U:85, V:86,
      W:87, X:88, Y:89, Z:90,
      WINLEFT: 91,   WINRIGHT: 92,  SELECT: 93,    NUM0: 96,
      NUM1: 97,      NUM2: 98,      NUM3: 99,      NUM4: 100,
      NUM5: 101,     NUM6: 102,     NUM7: 103,     NUM8: 104,
      NUM9: 105,     MULTIPLY: 106, ADD: 107,      SUBTRACT: 109,
      DECIMAL: 110,  DIVIDE: 111,   F1: 112,       F2: 113,
      F3: 114,       F4: 115,       F5: 116,       F6: 117,
      F7: 118,       F8: 119,       F9: 120,       F10: 121,
      F11: 122,      F12: 123,      NUMLOCK: 144,  SCROLLLOCK: 145,
      SEMICOLON: 186,EQUAL: 187,    COMMA: 188,    DASH: 189,
      PERIOD: 190,   FORWARDSLASH: 191,            GRAVEACCENT: 192,
      OPENBRACKET: 219,             BACKSLASH: 220,
      CLOSEBRACKET: 221,            QUOTE: 222
};

/* true - will be handled also by default handler and for false - will not (if you wanna disable some keys) */
function pressedKeyHandler(key){
     if (k != Keys.COMMA || k != Keys.DASH) return true;
     //handle pressed button here         
     return true; 
}

if (typeof window.event != 'undefined') // IE
  document.onkeydown = function() { return pressedKeyHandler(event.keyCode); }
else   // FireFox/Opera/Others 
  document.onkeypress = function(e) { return pressedKeyHandler(e.keyCode); }

</script>

我可能是错的,但似乎 IE 更好地处理 onkeydown 事件而不是 onkeypress。

<script type="text/javascript">
var Keys = {
      BACKSPACE: 8,  TAB: 9,        ENTER: 13,     SHIFT: 16,
      CTRL: 17,      ALT: 18,       PAUSE: 19,     CAPS: 20,
      ESC: 27,       PAGEUP: 33,    PAGEDN: 34,    END: 35,
      HOME: 36,      LEFT: 37,      UP: 38,        RIGHT: 39,
      DOWN: 40,      INSERT: 45,    DELETE: 46,       
      n0: 48, n1: 49, n2: 50, n3: 51, n4: 52,
      n5: 53, n6: 54, n7: 55, n8: 56, n9: 57,
      A:65, B:66, C:67, D:68, E:68, F:70, G:71, H:72, I:73, J:74, K:75,
      L:76, M:77, N:78, O:79, P:80, Q:81, R:82, S:83, T:84, U:85, V:86,
      W:87, X:88, Y:89, Z:90,
      WINLEFT: 91,   WINRIGHT: 92,  SELECT: 93,    NUM0: 96,
      NUM1: 97,      NUM2: 98,      NUM3: 99,      NUM4: 100,
      NUM5: 101,     NUM6: 102,     NUM7: 103,     NUM8: 104,
      NUM9: 105,     MULTIPLY: 106, ADD: 107,      SUBTRACT: 109,
      DECIMAL: 110,  DIVIDE: 111,   F1: 112,       F2: 113,
      F3: 114,       F4: 115,       F5: 116,       F6: 117,
      F7: 118,       F8: 119,       F9: 120,       F10: 121,
      F11: 122,      F12: 123,      NUMLOCK: 144,  SCROLLLOCK: 145,
      SEMICOLON: 186,EQUAL: 187,    COMMA: 188,    DASH: 189,
      PERIOD: 190,   FORWARDSLASH: 191,            GRAVEACCENT: 192,
      OPENBRACKET: 219,             BACKSLASH: 220,
      CLOSEBRACKET: 221,            QUOTE: 222
};

/* true - will be handled also by default handler and for false - will not (if you wanna disable some keys) */
function pressedKeyHandler(key){
     if (k != Keys.COMMA || k != Keys.DASH) return true;
     //handle pressed button here         
     return true; 
}

if (typeof window.event != 'undefined') // IE
  document.onkeydown = function() { return pressedKeyHandler(event.keyCode); }
else   // FireFox/Opera/Others 
  document.onkeypress = function(e) { return pressedKeyHandler(e.keyCode); }

</script>

I may be wrong but seems that for IE better to handle onkeydown event rather then onkeypress.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文