Take the 2-minute tour ×

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
add comment

4 Answers

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;


  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.

share|improve this answer
You need to use the keypress event to get the character typed, not the keydown event. –  Tim Down Sep 10 '10 at 8:58
@Tim: According to the quriksmode article, 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 Sep 10 '10 at 11:42
With all due respect to PPK, that particular quirksmode table is nothing like as helpful as 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 Sep 10 '10 at 14:09
@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 Sep 10 '10 at 14:50
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 Sep 10 '10 at 15:25
show 2 more comments

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) {
share|improve this answer
add comment

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


See MDN for more info.

share|improve this answer
add comment

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


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

share|improve this answer
I was going to suggest charCode as well, but there are some browsers that don't support it: quirksmode.org/js/keys.html –  Daniel Vassallo Sep 10 '10 at 3:51
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 Sep 10 '10 at 4:08
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 Sep 10 '10 at 9:13
add comment

Your Answer


