views:

54

answers:

4

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!

+5  A: 

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.

Daniel Vassallo
You need to use the `keypress` event to get the character typed, not the `keydown` event.
Tim Down
@Tim: According to [the quriksmode article](http://www.quirksmode.org/js/keys.html), the `keydown` event seems the most reliable. The main difference is that when using the `keydown` event you'd receive the same `keyCode` for lower case and upper case letters (because you're pressing the same key). If this is acceptable or not, depends on what the OP is building. Am I missing something else?
Daniel Vassallo
With all due respect to PPK, that particular quirksmode table is nothing like as helpful as http://unixpapa.com/js/key.html. The paragraph headed "keyCode and charCode" in quirksmode does contain the crux of it though: `keydown` and `keyup` events simply don't contain character information. Any success you may seem to have with it is coincidental and unreliable. If you're worried about Opera's lack of support for `charCode`, fortunately Opera supports the `which` property, which since it works in all non-IE browsers is more useful than `charCode` and is not covered by quirksmode.
Tim Down
@Tim: Interesting points. However, if we don't care about character information, and only require key codes (such as when implementing the inputs of a browser game), do you see any problems in my example?
Daniel Vassallo
If you don't care about character information then there's no problem with using `keydown`, but in that case `keyCode` works well in all browsers so there's no need to look at any other property. The OP does explicitly state that he/she is interested in the character information though.
Tim Down
@Tim: I didn't read that as an explicit request from the OP, but you're probably right :)... I +1ed your answer earlier, and cited it in my answer, since it's currently the top voted one.
Daniel Vassallo
Daniel: thanks.
Tim Down
+1  A: 

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 W3Schools for more info.

Christopher Scott
A: 

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).

Delan Azabani
I was going to suggest `charCode` as well, but there are some browsers that don't support it: http://www.quirksmode.org/js/keys.html
Daniel Vassallo
This is a difficult situation. On the one hand, for compliant browsers, keycode always is the keycode, and charcode is always the Unicode value. On the non-compliant browsers, charcode does not exist, and keycode varies between the keycode or the Unicode value depending on the type of event. Of course, we want the Unicode value, but that can never be seen in keycode on compliant browsers. Depending on the context of the website being written and the visitor demographic, I guess it's time to move on and ignore these blatantly non-compliant browsers.
Delan Azabani
Event handler for which event? `charCode` only exists in `keypress` events. For IE, `keyCode` in a `keypress` event is the character code and you don't get keypress events in IE for non-printable keys, so you can get character codes fairly reliably. See my answer.
Tim Down
+1  A: 

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));
    }
};
Tim Down