简体   繁体   English

是向上,向下,向左,向右箭头键码始终相同吗?

[英]Are Up, Down, Left, Right Arrow KeyCodes always the same?

As the title suggests, in my code I use the following codes: 正如标题所示,在我的代码中我使用以下代码:

  • Left: 37 左: 37
  • Up: 38 上: 38
  • Right: 39 右: 39
  • Down: 40 下来: 40

And check for those key codes to determine my action. 并检查这些关键代码以确定我的行动。 My question is, do those always remain the same? 我的问题是,那些总是保持不变吗? If I were to use a DVORAK keyboard, or a non-English keyboard, would those key codes remain the same? 如果我使用DVORAK键盘或非英语键盘,这些键码是否保持不变?

Along the same line, is there a preferred method for detecting those keystrokes? 沿着同一条线,是否有一种检测这些击键的首选方法?

Currently, I do it as follows: 目前,我这样做:

    var Key = {
        _pressed: {},
        LEFT: 37,
        UP: 38,
        RIGHT: 39,
        DOWN: 40,

        isDown: function (keyCode) {
            return this._pressed[keyCode];
        },

        onKeydown: function (event) {
            this._pressed[event.keyCode] = true;

            if (Key.isDown(Key.UP))
                //do up action
            else if (Key.isDown(Key.DOWN)) {
                //do down action
            }
            delete this._pressed[event.keyCode];
        }
    };

Yes, arrow key keycodes are always the same, regardless of the keyboard layout. 是的,无论键盘布局如何,箭头键键代码始终相同。

I regularly use different keyboard layouts (Dvorak, Russian, Ukrainian, Hebrew, Spanish) and I have tried all of them in JavaScript and they give consistent results for the arrow keys. 我经常使用不同的键盘布局(Dvorak,俄语,乌克兰语,希伯来语,西班牙语),我已经在JavaScript中尝试了所有这些,并且它们为箭头键提供了一致的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM