15

What's the easiest way to find the keycode for a specific key press?

Are there any good online tools that just capture any key event and show the code?

I want to try and find the key codes for special keys on a mobile device with a web browser, so an online tool would be great.

Acorn
  • 49,061
  • 27
  • 133
  • 172
  • [W3C DOM Level 3 KeyboardEvent code Values](https://www.w3.org/TR/2015/WD-DOM-Level-3-Events-code-20150428/) – davidcondrey Jun 02 '17 at 05:44
  • Just a side note: using `keyCode` is [not recommended anymore](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode). You can use [code](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code) or [key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key), depending on your use case. – zunnzunn May 10 '22 at 06:17

12 Answers12

21

    $(function () {
      $(document).keyup(function (e) {
         console.log(e.keyCode);
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Here's your online tool.

trincot
  • 317,000
  • 35
  • 244
  • 286
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
5

Just googled and result

function displayunicode(e) {
  var unicode = e.keyCode ? e.keyCode : e.charCode;
  console.log(unicode);
}
<form>
  <input type="text" size="2" maxlength="1" onkeyup="displayunicode(event); this.select()" />
</form>
Ahmet Kakıcı
  • 6,294
  • 4
  • 37
  • 49
4

If you are looking for an online tool, https://keyjs.dev is probably the best option available:

  • By default, it displays e.key, e.code, e.which and e.keyCode for the keydown event.

  • If you need more than that, you can expand it and you will get a table with more properties for keydown, keypress and keyup.

  • If you still need more than that, open DevTools and you will see all the keys you press are logged to the console in a table-like style. Just click the row you are interested in to expand it and see the whole event object.

Key.js \ JavaScript KeyboardEvent's key codes & key identifiers

Disclaimer: I'm the author.

Danziger
  • 19,628
  • 4
  • 53
  • 83
3

As in, what keyboard events reports based on what keys are pressed

  $("#textinput").keydown(function(e) {
    e.keyCode; // this value
  });

Try Here for all the key Events and These are the mobile key Events

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
srikanth_yarram
  • 957
  • 9
  • 16
2

The bottom of http://www.quirksmode.org/js/keys.html can show the keycode of keys you have pressed for the selected keyboard events.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2

If you are only looking for keyCode you essentially don't need to get the keypress event, you can simply convert character to keyCode and vise versa:

Char to KeyCode, for instance A ("A").charCodeAt(0) returns 65. Here's the syntax.

If you already know the characters which their keycodes are needed, say 'ABCDEFGH', you only need a loop to get all key codes:

var text = "ABCDEFGH";
for (var i=0; i< text.length; i++){
 console.log(text[i] ,text.charCodeAt(i))
}

It's obvious that this method is not going to be used for obtaining key codes of shif, ctrl or Alt key in keyboard, if you need them stick with the method stated above which uses keypress event.

FYI, to convert keyCode to Char: String.fromCharCode(65) returns A.

Muhammad Musavi
  • 2,512
  • 2
  • 22
  • 35
1

Please try this.

$('input').on('keyup', function(e){
   var key_code = e.which || e.keyCode;
   console.log(key_code );
});

For better help please follow the below link http://www.w3schools.com/jsref/event_key_keycode.asp

Banty Roy
  • 914
  • 6
  • 23
1

Try this:

$('#myelement').keydown(function(event) {
  var code = event.keyCode;
  alert(code);
}
Aaron Hathaway
  • 4,280
  • 2
  • 19
  • 17
1

Try not to hard-code too many keycodes. Let the JS library convert them for you wherever possible:

var code = ev.keyCode,
    ascii = String.fromCharCode(code);
alert(ascii);
srikanth
  • 9
  • 2
alsuren
  • 168
  • 1
  • 2
1

Vanilla javascript + Alert:

document.addEventListener('keypress', function(e) {
  alert("Key: " + e.code + ", Code: " + e.charCode)
});

Vanilla javascript + console:

document.addEventListener('keypress', function(e) {
  console.log("Key: " + e.code + ", Code: " + e.charCode)
});
SynergyChen
  • 1,161
  • 9
  • 4
0

You can try use keycode.info to do this, it's an online tool.

Nicholas Smith
  • 674
  • 1
  • 13
  • 27
0
<script>
        document.addEventListener("keydown", function(e){
            console.log(e.keyCode);
        });
</script>

It will print the keycode of the key pressed on the document body in console.

ADARSH J J
  • 21
  • 2