84

What keycodes are available for JavaScript? If they're not the same for all browsers, please list the keycodes for each browser.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
  • So for complete you want like the whole unicode range? You should specify what do you mean by "complete". – Jakub Hampl Apr 09 '11 at 06:10
  • By complete I mean containing all keycode numbers. The examples I gave give the numbers 8,9,13,16,17... Thus skipping some numbers. – Web_Designer Apr 09 '11 at 06:14
  • 3
    I do believe that there are a countable number of characters for which JavaScript keycodes are assigned. – Web_Designer Apr 09 '11 at 06:21
  • 7
    Why not create such a table yourself using JavaScript? – pimvdb Apr 09 '11 at 07:12
  • 1
    @pimvdb Took your advice! http://stackoverflow.com/a/14905611/552067 – Web_Designer Feb 16 '13 at 00:45
  • @pimvdb watch out, different browsers give different key codes in JavaScript. If you try it in JavaScript be sure to check FireFox and non-FireFox, and be especially wary for key codes 59 61 173 186 187 189. – Mishax May 30 '14 at 10:14

7 Answers7

59

keyCodes are different from the ASCII values. For a complete keyCode reference, see http://unixpapa.com/js/key.html

For example, Numpad numbers have keyCodes 96 - 105, which corresponds to the beginning of lowercase alphabet in ASCII. This could lead to problems in validating numeric input.

Soumya
  • 13,677
  • 6
  • 34
  • 49
user736373
  • 599
  • 1
  • 3
  • 2
  • 2
    The page linked to does not contain a complete reference. It adequately explains that the keycodes depend on the keyboard and the meanings of keycodes or keycode combinations depend on software that interprets them. So there can be no full list. – Jukka K. Korpela Sep 17 '13 at 13:23
  • 9
    [http://www.javascriptkeycode.com](http://www.javascriptkeycode.com) has a handy key code generator I've used recently to test different keyboard combinations. – Raz Wilson Mar 07 '14 at 17:59
32

Followed @pimvdb's advice, and created my own:

http://daniel-hug.github.io/characters/

Be patient, as it takes a few seconds to generate an element for each of the 65536 characters that have a JavaScript keycode.

Community
  • 1
  • 1
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
  • 13
    Horrible site, lets my browser crash everytime. Dont use this! – Florian Leitgeb Mar 05 '14 at 18:57
  • @Floeee Sorry, it currently generates a table row for all 65536 characters that have a javascript keycode. It does this very efficiently, but it still takes 10-20 seconds. I might change it so it loads 1000 just to start, then more upon request. Answer edited to explain this. – Web_Designer Mar 05 '14 at 23:05
  • Why don't you display a simple text message at the start of the page explaining this? – Jared Mar 18 '14 at 03:38
  • 4
    It may be more accurate to say "it takes 10-20 seconds to irreversibly crash your browser until you terminate the process." – Dissident Rage Mar 26 '14 at 19:37
  • Okay, got around the loading and crashing. BUT....it doesnt work. Atleast not with delete or backspace. Have you thought about not loading them all at once, but rather catch the event, dynamically post it to a php that pulls it out of the db and then displays the results? This is a really cool idea. Clean it up. Chances are, noone will be going there to grab anything other than a few keycodes. Alternative for nonSQL user approach....put all the results in a php page and use ajax/jquery to send the user input and pull the results. Then you dont have to have a loading issue on arrival. –  May 01 '14 at 22:33
  • I looked at the code and I don't believe it's right to say it does it *very efficiently*. However, in lieu of myself not having a replacement solution... carry on. – alex Jun 24 '14 at 05:02
  • 4
    These are charCodes not keyCodes. keyCodes are listed under 3.3 section of http://unixpapa.com/js/key.html – Semra Dec 20 '14 at 15:59
  • These are Unicode charCodes. For JavaScript keyboardEvent keyCodes, use [@Nimphious's](http://stackoverflow.com/a/12476699/3471286) script. It is not complete for non-US keyboards though, be aware. – Gui Imamura Sep 15 '15 at 13:24
  • Yeah..., it crashed my browser too.. – Ari Sep 18 '15 at 01:36
19

I needed something like this for a game's control configuration UI, so I compiled a list for the standard US keyboard layout keycodes and mapped them to their respective key names.

Here's a fiddle that contains a map for code -> name and visi versa: http://jsfiddle.net/vWx8V/

If you want to support other key layouts you'll need to modify these maps to accommodate for them separately.

That is unless you were looking for a list of keycode values that included the control characters and other special values that are not (or are rarely) possible to input using a keyboard and may be outside of the scope of the keydown/keypress/keyup events of Javascript. Many of them are control characters or special characters like null (\0) and you most likely won't need them.

Notice that the number of keys on a full keyboard is less than many of the keycode values.

Nimphious
  • 4,889
  • 1
  • 17
  • 17
8

http://keycodes.atjayjo.com/

This app is just awesome. It is essentially a virtual keyboard that immediately shows you the keycode pressed on a standard US keyboard.

jiminikiz
  • 2,867
  • 1
  • 25
  • 28
5

Here is a complete list--I believe: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

HighHopes
  • 2,014
  • 3
  • 23
  • 33
  • I created a gist with a KEY_CODE constant based on this link: https://gist.github.com/DJDaveMark/eb9725825ac3a87616c66d05a8cf8f4a – DJDaveMark Jun 29 '17 at 09:40
3

Here are some useful links:

The 2nd column is the keyCode and the html column shows how it will displayed. You can test it here.

1

One possible answer will be given when you run this snippet.

document.write('<table>')
for (var i = 0; i < 250; i++) {
  document.write('<tr><td>' + i + '</td><td>' + String.fromCharCode(i) + '</td></tr>')
}
document.write('</table>')
td {
  border: solid 1px;
  padding: 1px 12px;
  text-align: right;
}
table {
  border-collapse: collapse;
}
* {
  font-family: monospace;
  font-size: 1.1em;
}
fiatjaf
  • 11,479
  • 5
  • 56
  • 72