最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Are Up, Down, Left, Right Arrow KeyCodes always the same? - Stack Overflow

programmeradmin14浏览0评论

As the title suggests, in my code I use the following codes:

  • Left: 37
  • Up: 38
  • Right: 39
  • Down: 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?

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];
        }
    };

As the title suggests, in my code I use the following codes:

  • Left: 37
  • Up: 38
  • Right: 39
  • Down: 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?

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];
        }
    };
Share Improve this question edited Dec 31, 2013 at 21:24 Giacomo1968 26.1k11 gold badges76 silver badges105 bronze badges asked Dec 31, 2013 at 21:22 wjhguitarmanwjhguitarman 1,0731 gold badge11 silver badges27 bronze badges 3
  • 2 unixpapa.com/js/key.html seems to treat the topic exhaustively. – i_am_jorf Commented Dec 31, 2013 at 21:24
  • @jeffamaphone But ignores different keyboard layouts completely, only talking (exhaustively) about browser differences. – Adam Smith Commented Dec 31, 2013 at 21:28
  • Coming off the link jeffamaphone posted..it listed pseudo ascii values..which should remain constant across keyboard layouts/languages? – wjhguitarman Commented Dec 31, 2013 at 21:40
Add a comment  | 

1 Answer 1

Reset to default 28

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论