如何在 JavaScript 中按下键并放入数组?

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

How to get the key pressed and put into array in JavaScript?

javascriptkeyboardkeypress

提问by OOProg

How do I get the key that was pressed and, instead of returning the key code, put that key into an array?

如何获取按下的键,而不是返回键代码,而是将该键放入数组中?

For example, the user will press 'a'. Then, the code will put 'a' - not the keycode for the character - into an array.

例如,用户将按“a”。然后,代码会将“a”——而不是字符的键码——放入一个数组中。

Thanks in advance!

提前致谢!

回答by Daniel Vassallo

What about something like this?

像这样的事情怎么办?

var your_array = [];

document.onkeydown = function (e) {
  var keyPress;

  if (typeof event !== 'undefined') {
    keyPress = event.keyCode;
  }
  else if (e) {
    keyPress = e.which;
  }

  your_array.push(String.fromCharCode(keyPress));

  return false;   // Prevents the default action
};


UPDATE:If you require accurate character information (such as, the distinction of uppercase from lowercase, and other things), make sure to check out @Tim Down'scomments below and his other answer.

更新:如果您需要准确的字符信息(例如,大写与小写的区别等),请务必查看下面@Tim Down 的评论和他的其他答案

回答by Tim Down

You need the keypressevent for this. keydownand keyupcannot be used reliably to get character information. An excellent and detailed explanation of JavaScript key events is at http://unixpapa.com/js/key.html

你需要这个keypress事件。keydown并且keyup不能可靠地用于获取字符信息。对 JavaScript 关键事件的出色而详细的解释位于http://unixpapa.com/js/key.html

var charsTyped = [];

document.onkeypress = function(evt) {
    evt = evt || window.event;

    // Ensure we only handle printable keys
    var charCode = typeof evt.which == "number" ? evt.which : evt.keyCode;

    if (charCode) {
        charsTyped.push(String.fromCharCode(charCode));
    }
};

回答by Christopher Scott

Daniel's answer is perfect, but if you want to get the actual character (not the numerical code), you can use this function:

Daniel 的回答是完美的,但是如果你想得到实际的字符(不是数字代码),你可以使用这个函数:

String.fromCharCode(code);

See MDNfor more info.

有关更多信息,请参阅MDN

回答by Delan Azabani

In your event handler (assuming eis the event object):

在您的事件处理程序中(假设e是事件对象):

myarray.push(String.fromCharCode(e.charCode));

Notice how fromCharCodereturns the character given a Unicode character code. Also notice how I used charCodeinstead of keyCodeas it's more correct in returning the character code, which sometimes is different to the keycode (you want the character).

请注意如何fromCharCode返回给定 Unicode 字符代码的字符。还要注意我是如何使用charCode而不是keyCode因为它在返回字符代码时更正确,它有时与键码不同(您需要字符)。

回答by B T

I wrote a library called keysightto translate keyboard events into keys and characters.

我编写了一个名为keysight的库来将键盘事件转换为键和字符。

var yourKeyArray = []
node.addEventListener("keydown", function(event) {
    var key = keysight(event).key      // ignores shift keys, so 'A' is given as 'a'
    // var char = keysight(event).char // only characters, and differentiates between 'A' and 'a'

    yourKeyArray.push(key)
})