javascript 浏览器键码列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7694486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:58:53  来源:igfitidea点击:

Browser key code list?

javascriptkeyboardcross-browser

提问by Isaiah

I know this is probably going to be in vain but I want to see if there is an answer. I'm making an HTML5 game and I'm trying to get keyboard input. Please tell me someone knows something google doesn't. Hopefully at least it'll stress how much keyboard events and keycodes need to be made more cross-browser.

我知道这可能是徒劳的,但我想看看是否有答案。我正在制作一个 HTML5 游戏,我正在尝试获得键盘输入。请告诉我有人知道谷歌不知道的东西。希望至少它会强调需要使多少键盘事件和键码更加跨浏览器。

Please tell me there is some kind of object in the javascript api that lists each key's keycode. And if not, why hasn't it been done yet, just being able to grab a key's code would make our jobs so much easier. No more testing the inconsistencies per browser/os.

请告诉我 javascript api 中有某种对象列出了每个键的键码。如果没有,为什么还没有完成,仅仅能够获取密钥的代码将使我们的工作变得更加容易。不再测试每个浏览器/操作系统的不一致。

And if not that (this is probably more in vain) is there a way to redefine the default keycodes to our own custom keycodes?

如果不是这样(这可能更徒劳)有没有办法将默认键码重新定义为我们自己的自定义键码?

I don't know why more work hasn't been done to make this more convenient?

我不知道为什么没有做更多的工作来使这更方便?

回答by aziz punjani

I find thispage to be very helpful. Also quirks modehas a good article on it. Fact of the matter is that there's browser inconsistencies and a javascript framework like jQuery help with that.

我觉得这个页面非常有帮助。此外怪癖模式上有一个很好的文章。事实是存在浏览器不一致的问题,而像 jQuery 这样的 javascript 框架可以帮助解决这个问题。

回答by zzzzBov

JavaScript does not include a built-in hash of keycode-to-keyname pairings.

JavaScript 不包含键码到键名配对的内置哈希。

It's largely unnecessary because JS uses the same keycodes as are reported by the Operating System. Adding a hash would only hinder the performance unnecessarily.

这在很大程度上是不必要的,因为 JS 使用与操作系统报告的相同的键码。添加散列只会不必要地阻碍性能。

The program doesn't need to know what the name of the key was, it just needs to know what action to take.

程序不需要知道密钥的名称是什么,它只需要知道要采取什么行动。

Instead of writing code like:

而不是编写如下代码:

if (e.keyCode === customKeyList.UP)
{
  doUp();
}

You can simply set your own hash of actions:

您可以简单地设置自己的操作哈希:

//before the event
actions = {
  '38': function () {
    //do UP stuff here
  },
  '40': function () {
    //do DOWN stuff here
  }
};
//during the event
if (actions[e.keyCode]) {
  actions[e.keyCode]();
}

At no point in that example is it necessary for the computerto know the name of the key, but for convenience it may be useful to write it as:

在该示例中,计算机没有必要知道密钥的名称,但为了方便起见,将其写为:

actions[keys.UP] = function () {...};
actions[keys.DOWN] = function () {...};

but you'll need to define your own keycode-to-keyname pairing object.

但您需要定义自己的键码到键名配对对象。



I recently discovered that jQuery UI has a list of some commonly used keycodes ($.ui.keyCode):

我最近发现 jQuery UI 有一些常用键码 ( $.ui.keyCode)的列表:

keyCode: {
    ALT: 18,
    BACKSPACE: 8,
    CAPS_LOCK: 20,
    COMMA: 188,
    COMMAND: 91,
    COMMAND_LEFT: 91, // COMMAND
    COMMAND_RIGHT: 93,
    CONTROL: 17,
    DELETE: 46,
    DOWN: 40,
    END: 35,
    ENTER: 13,
    ESCAPE: 27,
    HOME: 36,
    INSERT: 45,
    LEFT: 37,
    MENU: 93, // COMMAND_RIGHT
    NUMPAD_ADD: 107,
    NUMPAD_DECIMAL: 110,
    NUMPAD_DIVIDE: 111,
    NUMPAD_ENTER: 108,
    NUMPAD_MULTIPLY: 106,
    NUMPAD_SUBTRACT: 109,
    PAGE_DOWN: 34,
    PAGE_UP: 33,
    PERIOD: 190,
    RIGHT: 39,
    SHIFT: 16,
    SPACE: 32,
    TAB: 9,
    UP: 38,
    WINDOWS: 91 // COMMAND
}

回答by Andrey M.

The keycodes.jsand keyhandler.jsclasses from Google Closure library have a list of many key codes and useful comments about differencies across different browsers.

keycodes.jskeyhandler.js从谷歌封库类有许多关键的代码和有关跨不同的浏览器differencies有用的意见清单。

回答by kennebec

You can find the key bindings for most browsers by capturing a keypress and examining the event object- it may have a set of virtual key bindings.

您可以通过捕获按键并检查事件对象来找到大多数浏览器的键绑定——它可能有一组虚拟键绑定。

Mozilla returns this set-

Mozilla 返回了这个集合——

Property    Value
CANCEL=     3
HELP=   6
BACK_SPACE=     8
TAB=    9
CLEAR=  12
RETURN=     13
ENTER=  14
SHIFT=  16
CONTROL=    17
ALT=    18
PAUSE=  19
CAPS_LOCK=  20
KANA=   21
HANGUL=     21
JUNJA=  23
FINAL=  24
HANJA=  25
KANJI=  25
ESCAPE=     27
CONVERT=    28
NONCONVERT=     29
ACCEPT=     30
MODECHANGE=     31
SPACE=  32
PAGE_UP=    33
PAGE_DOWN=  34
END=    35
HOME=   36
LEFT=   37
UP=     38
RIGHT=  39
DOWN=   40
SELECT=     41
PRINT=  42
EXECUTE=    43
PRINTSCREEN=    44
INSERT=     45
DELETE=     46
0=  48
1=  49
2=  50
3=  51
4=  52
5=  53
6=  54
7=  55
8=  56
9=  57
SEMICOLON=  59
EQUALS=     61
A=  65
B=  66
C=  67
D=  68
E=  69
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
CONTEXT_MENU=   93
SLEEP=  95
NUMPAD0=    96
NUMPAD1=    97
NUMPAD2=    98
NUMPAD3=    99
NUMPAD4=    100
NUMPAD5=    101
NUMPAD6=    102
NUMPAD7=    103
NUMPAD8=    104
NUMPAD9=    105
MULTIPLY=   106
ADD=    107
SEPARATOR=  108
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
F13=    124
F14=    125
F15=    126
F16=    127
F17=    128
F18=    129
F19=    130
F20=    131
F21=    132
F22=    133
F23=    134
F24=    135
NUM_LOCK=   144
SCROLL_LOCK=    145
COMMA=  188
PERIOD=     190
SLASH=  191
BACK_QUOTE=     192
OPEN_BRACKET=   219
BACK_SLASH=     220
CLOSE_BRACKET=  221
QUOTE=  222
META=   224