Javascript 如何查找特定密钥的密钥代码

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

How to find the key code for a specific key

javascriptjqueryeventskeyboardkeyboard-events

提问by Acorn

What's the easiest way to find the keycode for a specific key press?

查找特定按键的键码的最简单方法是什么?

Are there any good online tools that just capture any key event and show the code?

有没有什么好的在线工具可以捕获任何关键事件并显示代码?

I want to try and find the key codes for special keys on a mobile device with a web browser, so an online tool would be great.

我想尝试在带有网络浏览器的移动设备上查找特殊键的键码,因此在线工具会很棒。

回答by Bertrand Marron

    $(function () {
      $(document).keyup(function (e) {
         console.log(e.keyCode);
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Here's your online tool.

这是您的在线工具。

回答by Ahmet Kak?c?

Just googled and result

刚刚谷歌搜索结果

function displayunicode(e) {
  var unicode = e.keyCode ? e.keyCode : e.charCode;
  console.log(unicode);
}
<form>
  <input type="text" size="2" maxlength="1" onkeyup="displayunicode(event); this.select()" />
</form>

回答by sri_bb

As in, what keyboard events reports based on what keys are pressed

例如,根据按下的键报告什么键盘事件

  $("#textinput").keydown(function(e) {
    e.keyCode; // this value
  });

Try Here for all the key Eventsand These are the mobile key Events

在这里尝试所有关键事件这些是移动关键事件

回答by kennytm

The bottom of http://www.quirksmode.org/js/keys.htmlcan show the keycode of keys you have pressed for the selected keyboard events.

http://www.quirksmode.org/js/keys.html的底部可以显示您为所选键盘事件按下的键的键码。

回答by Aaron Hathaway

Try this:

尝试这个:

$('#myelement').keydown(function(event) {
  var code = event.keyCode;
  alert(code);
}

回答by alsuren

Try not to hard-code too many keycodes. Let the JS library convert them for you wherever possible:

尽量不要硬编码太多的键码。让 JS 库尽可能为您转换它们:

var code = ev.keyCode,
    ascii = String.fromCharCode(code);
alert(ascii);

回答by Banty Roy

Please try this.

请试试这个。

$('input').on('keyup', function(e){
   var key_code = e.which || e.keyCode;
   console.log(key_code );
});

For better help please follow the below link http://www.w3schools.com/jsref/event_key_keycode.asp

如需更好的帮助,请点击以下链接http://www.w3schools.com/jsref/event_key_keycode.asp

回答by Mohammad Musavi

If you are only looking for keyCode you essentially don't need to get the keypress event, you can simply convert character to keyCode and vise versa:

如果您只是在寻找 keyCode,您基本上不需要获取 keypress 事件,您可以简单地将字符转换为 keyCode,反之亦然:

Char to KeyCode, for instance A ("A").charCodeAt(0)returns 65. Here's the syntax.

Char 到 KeyCode,例如 A ("A").charCodeAt(0)返回 65。 是语法。

If you already know the characters which their keycodes are needed, say 'ABCDEFGH', you only need a loop to get all key codes:

如果您已经知道需要其键码的字符,请说“ABCDEFGH”,您只需要一个循环即可获取所有键码:

var text = "ABCDEFGH";
for (var i=0; i< text.length; i++){
 console.log(text[i] ,text.charCodeAt(i))
}

It's obvious that this method is not going to be used for obtaining key codes of shif, ctrl or Alt key in keyboard, if you need them stick with the method stated above which uses keypress event.

很明显,此方法不会用于获取键盘中 shif、ctrl 或 Alt 键的键码,如果您需要它们,请坚持使用上述使用按键事件的方法。

FYI, to convert keyCode to Char: String.fromCharCode(65)returns A.

仅供参考,将keyCode转换为 CharString.fromCharCode(65)返回 A。

回答by SynergyChen

Vanilla javascript + Alert:

香草 javascript + 警报:

document.addEventListener('keypress', function(e) {
  alert("Key: " + e.code + ", Code: " + e.charCode)
});

Vanilla javascript + console:

香草 javascript + 控制台:

document.addEventListener('keypress', function(e) {
  console.log("Key: " + e.code + ", Code: " + e.charCode)
});