什么是 JavaScript KeyCode?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5603195/
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-08-23 18:00:10  来源:igfitidea点击:

What are the JavaScript KeyCodes?

javascriptkeycode

提问by Web_Designer

What keycodes are available for JavaScript? If they're not the same for all browsers, please list the keycodes for each browser.

哪些键码可用于 JavaScript?如果它们对所有浏览器都不同,请列出每个浏览器的键码。

采纳答案by Web_Designer

Followed @pimvdb's advice, and created my own:

遵循@pimvdb 的建议,并创建了我自己的建议

http://daniel-hug.github.io/characters/

http://daniel-hug.github.io/characters/

Be patient, as it takes a few seconds to generate an element for each of the 65536 characters that have a JavaScript keycode.

请耐心等待,因为为具有 JavaScript 键码的 65536 个字符中的每一个生成一个元素都需要几秒钟的时间。

回答by user736373

keyCodes are differentfrom the ASCII values. For a complete keyCode reference, see http://unixpapa.com/js/key.html

keyCodes不同于ASCII 值。有关完整的 keyCode 参考,请参阅http://unixpapa.com/js/key.html

For example, Numpad numbers have keyCodes 96 - 105, which corresponds to the beginning of lowercase alphabet in ASCII. This could lead to problems in validating numeric input.

例如,Numpad 数字的 keyCodes 为 96 - 105,对应于 ASCII 中小写字母的开头。这可能会导致验证数字输入时出现问题。

回答by Nimphious

I needed something like this for a game's control configuration UI, so I compiled a list for the standard US keyboard layout keycodes and mapped them to their respective key names.

我需要这样的东西来制作游戏的控制配置 UI,所以我为标准的美式键盘布局键码编译了一个列表,并将它们映射到它们各自的键名。

Here's a fiddle that contains a map for code -> name and visi versa: http://jsfiddle.net/vWx8V/

这是一个包含代码映射的小提琴 -> 名称,反之亦然:http: //jsfiddle.net/vWx8V/

If you want to support other key layouts you'll need to modify these maps to accommodate for them separately.

如果您想支持其他按键布局,您需要修改这些地图以分别适应它们。

That is unless you were looking for a list of keycode values that included the control characters and other special values that are not (or are rarely) possible to input using a keyboard and may be outside of the scope of the keydown/keypress/keyupevents of Javascript. Many of them are control characters or special characters like null(\0) and you most likely won't need them.

也就是说,除非你要找的键码值,其中包括控制字符和不在(或很少),可使用键盘输入,并且可以在范围之外的其他特殊值的列表keydown/ keypress/keyup的Javascript事件。其中许多是控制字符或特殊字符,例如null( \0),您很可能不需要它们。

Notice that the number of keys on a full keyboard is less than many of the keycode values.

请注意,全键盘上的键数少于许多键码值。

回答by jiminikiz

http://keycodes.atjayjo.com/

http://keycodes.atjayjo.com/

This app is just awesome. It is essentially a virtual keyboard that immediately shows you the keycode pressed on a standard US keyboard.

这个应用程序太棒了。它本质上是一个虚拟键盘,可以立即向您显示在标准美式键盘上按下的键码。

回答by Casey

回答by Casey

Here are some useful links:

以下是一些有用的链接:

The 2nd column is the keyCode and the html column shows how it will displayed. You can test it here.

第二列是 keyCode,html 列显示它将如何显示。你可以在这里测试它。

回答by fiatjaf

One possible answer will be given when you run this snippet.

运行此代码段时将给出一个可能的答案。

document.write('<table>')
for (var i = 0; i < 250; i++) {
  document.write('<tr><td>' + i + '</td><td>' + String.fromCharCode(i) + '</td></tr>')
}
document.write('</table>')
td {
  border: solid 1px;
  padding: 1px 12px;
  text-align: right;
}
table {
  border-collapse: collapse;
}
* {
  font-family: monospace;
  font-size: 1.1em;
}