How to find out what character key is pressed?

260,308

Solution 1

"Clear" JavaScript:

function myKeyPress(e){
  var keynum;

  if(window.event) { // IE                  
    keynum = e.keyCode;
  } else if(e.which){ // Netscape/Firefox/Opera                 
    keynum = e.which;
  }

  alert(String.fromCharCode(keynum));
}
<input type="text" onkeypress="return myKeyPress(event)" />

JQuery:

$("input").keypress(function(event){
  alert(String.fromCharCode(event.which)); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input/>

Solution 2

There are a million duplicates of this question on here, but here goes again anyway:

document.onkeypress = function(evt) {
    evt = evt || window.event;
    var charCode = evt.keyCode || evt.which;
    var charStr = String.fromCharCode(charCode);
    alert(charStr);
};

The best reference on key events I've seen is http://unixpapa.com/js/key.html.

Solution 3

More recent and much cleaner: use event.key. No more arbitrary number codes!

NOTE: The old properties (.keyCode and .which) are Deprecated.

node.addEventListener('keydown', function(event) {
    const key = event.key; // "a", "1", "Shift", etc.
});

If you want to make sure only single characters are entered, check key.length === 1, or that it is one of the characters you expect.

Mozilla Docs

Supported Browsers

Solution 4

Try:

<table>
  <tr>
    <td>Key:</td>
    <td id="key"></td>
  </tr>
  <tr>
    <td>Key Code:</td>
    <td id="keyCode"></td>
  </tr>
  <tr>
    <td>Event Code:</td>
    <td id="eventCode"></td>
  </tr>
</table>
<script type="text/javascript">
  window.addEventListener("keydown", function(e) {
    //tested in IE/Chrome/Firefox
    document.getElementById("key").innerHTML = e.key;
    document.getElementById("keyCode").innerHTML = e.keyCode;
    document.getElementById("eventCode").innerHTML = e.code;
  })
</script>

*Note: this works in "Run code snippet"

This website does the same as my code above: Keycode.info

Solution 5

Use this one:

function onKeyPress(evt){
  evt = (evt) ? evt : (window.event) ? event : null;
  if (evt)
  {
    var charCode = (evt.charCode) ? evt.charCode :((evt.keyCode) ? evt.keyCode :((evt.which) ? evt.which : 0));
    if (charCode == 13) 
        alert('User pressed Enter');
  }
}
Share:
260,308
xXx
Author by

xXx

Updated on July 08, 2022

Comments

  • xXx
    xXx almost 2 years

    I would like to find out what character key is pressed in a cross-browser compatible way in pure Javascript.

  • VoVaVc
    VoVaVc over 10 years
    this works ok for alphabetic chars, but what about dots/brakets and other typogtaphic symbols?
  • Tim Down
    Tim Down about 9 years
    @VoVaVc: It works for those too. The crucial thing is using the keypress event, which gives you a character code, rather than keyup or keydown which give you a key code.
  • Chris F Carroll
    Chris F Carroll almost 9 years
    Isn't this a near-exact duplicate of @Coyod's 2009 answer?
  • aljgom
    aljgom about 7 years
    e.which is deprecated. Use e.key instead, as in if(e.key == 'z')
  • Kelderic
    Kelderic over 6 years
    @aljgom, e.key still doesn't have full browser support.
  • Curtis
    Curtis over 5 years
    Doesn't work for symbols that you have to press shift to make, like !
  • Ray
    Ray over 5 years
    @AndyMercer key is now supported by all major browsers: developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/…
  • Zuabi
    Zuabi about 4 years
    The keypress event seems to be deprecated w3.org/TR/DOM-Level-3-Events/#event-type-keypress
  • merrybot
    merrybot almost 4 years
    Is this for node.js?
  • Gibolt
    Gibolt almost 4 years
    No, node just means any DOM element. If you had Node.js connecting to a UI, I suppose it would work
  • Irrmich
    Irrmich almost 4 years
    Uncaught TypeError: Mousetrap.record is not a function at recordSequence (****.html:12) at HTMLButtonElement.onclick (****.html:20)
  • Angelotti
    Angelotti over 3 years
    keydown for some mobile device doesn't work it return undefine
  • Danziger
    Danziger over 3 years
    Here's another similar site but with a few additional options: keyjs.dev
  • Paolo Benvenuto
    Paolo Benvenuto over 2 years
    on chrome, xfce, italian keyboard, spanish layout, pressing "<" returns "/", which is the key which is there in english keyboard. Why?!? some chrome versions ago it was not that!
  • MindRoasterMir
    MindRoasterMir over 2 years
    two negative votes but now explaination why it is voted negative. I think it help when you comment what is the problem with this answer. thanks
  • gomozor
    gomozor over 2 years
    easy to vote, I guess you bettter try this snippet on your own :) title is pretty common by the way