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

keyboard - How to get the key pressed and put into array in JavaScript? - Stack Overflow

programmeradmin6浏览0评论

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.

Thanks in advance!

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.

Thanks in advance!

Share Improve this question edited Sep 11, 2010 at 18:31 Daniel Vassallo 344k72 gold badges512 silver badges446 bronze badges asked Sep 10, 2010 at 3:39 OOProgOOProg 1891 gold badge5 silver badges16 bronze badges 1
  • Why do you use key property? To try this: document.onkeydown = function(e) {alert(e.key);}; It is not need to convert keyCode to characters. – MURATSPLAT Commented Dec 6, 2014 at 15:38
Add a comment  | 

5 Answers 5

Reset to default 7

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's comments below and his other answer.

You need the keypress event for this. keydown and keyup cannot be used reliably to get character information. An excellent and detailed explanation of JavaScript key events is at 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));
    }
};

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

String.fromCharCode(code);

See MDN for more info.

In your event handler (assuming e is the event object):

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

Notice how fromCharCode returns the character given a Unicode character code. Also notice how I used charCode instead of keyCode as it's more correct in returning the character code, which sometimes is different to the keycode (you want the character).

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

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)
})
发布评论

评论列表(0)

  1. 暂无评论