KeyboardEvent Value (keyCodes, MetaKey, Etc) - CSS-Tricks

When a KeyboardEvent fires, you can test which key was pressed because that event contains information you can write logic against.

document.addEventListener("keydown", function(event) { console.log(event.which); })

For example, by pressing a, you’ll get 65. Apparently it’s best to write logic against which, as keyCode and charCode are complicated:

The event.which property normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input.

And:

In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. ‘a’), charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account whether the Shift key is held down). Otherwise, the code of the pressed key is stored in keyCode.

Tester Tool

Keycode values

Here’s a table that contains the values from event.which.

Key Code
backspace 8
tab 9
enter 13
shift 16
ctrl 17
alt 18
pause/break 19
caps lock 20
escape 27
(space) 32
page up 33
page down 34
end 35
home 36
left arrow 37
up arrow 38
right arrow 39
down arrow 40
insert 45
delete 46
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
a 65
b 66
c 67
d 68
Key Code
e 69
f 70
g 71
h 72
i 73
j 74
k 75
l 76
m 77
n 78
o 79
p 80
q 81
r 82
s 83
t 84
u 85
v 86
w 87
x 88
y 89
z 90
left window key 91
right window key 92
select key 93
numpad 0 96
numpad 1 97
numpad 2 98
numpad 3 99
numpad 4 100
numpad 5 101
numpad 6 102
numpad 7 103
Key Code
numpad 8 104
numpad 9 105
multiply 106
add 107
subtract 109
decimal point 110
divide 111
f1 112
f2 113
f3 114
f4 115
f5 116
f6 117
f7 118
f8 119
f9 120
f10 121
f11 122
f12 123
num lock 144
scroll lock 145
semi-colon 186
equal sign 187
comma 188
dash 189
period 190
forward slash 191
grave accent 192
open bracket 219
back slash 220
close braket 221
single quote 222

Zell Liew noticed that 3 of these keycodes were different in Firefox than the rest of the browsers

  • ; is 59 in Firefox but 186 in other browsers.
  • = is 61 in Firefox but 187 in other browsers.
  • - is 173 in Firefox but 189 in other browsers.

These keycode values are only valid during in keydown and keyup events. On Mac, keypress events give you an completely different set of codes. For example:

Keyevent.which in keydownevent.which in keypress
a6597
b6698
c6799
Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.

Comments

  1. Ben Colegate Permalink to comment# June 26, 2012

    It is recommended by jQuery to use e.which over e.keyCode, as it gets normalised across all browsers.

    It also takes into account mouseup and mousedown.

    Not trying to be funny, just trying to help!!

    http://api.jquery.com/event.which/

    Reply
    • Ben Colegate Permalink to comment# June 26, 2012

      By mouseus, I clearly meant mouseup!

    • Chris Permalink to comment# June 27, 2012

      I agree with this, I stopped using e.keyCode along time ago when I found out e.which was more widely supported.

    • ardianzzz Permalink to comment# July 3, 2012

      What about JQuery Hotkeys plugin? https://github.com/tzuryby/jquery.hotkeys

  2. Hugo Permalink to comment# June 27, 2012

    Great tip Chris, thanks for sharing !

    I never figured it is that simple to know keyCodes with jQuery.

    Reply
  3. John Jimenez Permalink to comment# June 29, 2012

    Here is a decent article (a little out of date) about keycode support and the events that trigger. I am not affiliated with the link at all, just something I came across the other day and it happens to work here as well. http://unixpapa.com/js/key.html

    Reply
  4. JohnMotylJr Permalink to comment# March 11, 2013

    Dang, im still using this:

    var code = (e.keyCode ? e.keyCode : e.which); Reply
    • Chris Permalink to comment# November 7, 2014

      I still use that and always will until every browser uses the same property! Which will most likely never happen so I’d keep using it! I mean half these morons on here saying “Never knew it was that easy with jQuery” they must not know jQuery is a library of JavaScript -_- old developers will always out due all these newcomers, “jQuery” programmers. They make me laugh!

    • spyter Permalink to comment# June 12, 2015

      var code = e.keyCode || e.which; //is a shorter/cleaner version of what you have

    • J G Permalink to comment# November 25, 2015

      @spyter: just know that if e.keyCode === 0, then it will be falsey and fall through to e.which (which may not be a problem, but is a possibility)

    • Thato Permalink to comment# July 23, 2017

      hahahah very smart

  5. James Larsson Permalink to comment# September 12, 2013

    I found that the Apple command key is 224, which is not on this list.

    Reply
    • Michael Coyne Permalink to comment# June 3, 2015

      only on firefox in chrome it is 93

    • Michael Coyne Permalink to comment# June 3, 2015

      sorry meant 91

    • Pratyush Tewari Permalink to comment# September 11, 2017

      The left command key is 91 and the right command key 93. So in your code please check for both.

  6. saurabh Permalink to comment# April 15, 2014

    i want alt+q =keycode??

    Reply
    • Mottie Permalink to comment# July 25, 2014

      To detect that you’d need to look at the altKey value:

      if (event.altKey && event.which === 81) { // alt-q being used }
    • zixuan Permalink to comment# November 21, 2016

      i want alt+g =keycode??

  7. Mottie Permalink to comment# July 25, 2014

    @chriscoyier The table seems to be missing “space” with a event.which value of 32 :)

    Reply
  8. Crystal Permalink to comment# August 21, 2014

    What about the key codes for other languages?

    Reply
  9. Veronica Permalink to comment# September 18, 2014

    Just found out that, for event.which, only in FF, backspace is 8, and the arrow keys are 0. In Chrome, it doesn’t set a value for either.

    Reply
    • Aidan Permalink to comment# November 13, 2014

      You are using element.onkeypress, to get the arrow keys you use element.onkeydown .

  10. Ali.MD Permalink to comment# December 4, 2014

    Thank you for exact copy paste and don’t read even once ! I mean: ‘close braket’ just for fun ;)

    Reply
  11. Ali.MD Permalink to comment# December 4, 2014

    Please also add these: NUM_LOCK_MAC: 12, F13: 124, F14: 125, F15: 126, F16: 127, F17: 128, F18: 129, F19: 130,

    Reply
  12. Strateji oyunu Permalink to comment# January 12, 2015

    I wanting alt+q = keycode?

    Reply
  13. Ben Zörb Permalink to comment# January 14, 2015

    There are some conflicting keycodes which belong to two characters when using keyup or keydown: 188 , < 190 . > 191 / ? 192 ` ~ 219 [ { 220 \ | 221 ] } 222 ‘ “

    Reply
  14. Bhumi Permalink to comment# February 6, 2015

    I have tried following but not working

    var DisableArrowKeys = function(e){ if(e.keyCode == 40){ return false; } if(e.keyCode == 39){ return false; } return true; } $("#id").bind("keyup change", DisableArrowKeys); $("#id").bind("keydown change", DisableArrowKeys); Reply
    • sreeee Permalink to comment# March 22, 2016 $('#id').bind('keydown', function(e){ if(e.keyCode == '39' || e.keyCode == '40'){ e.preventDefault(); } });

      this will help you i think

    • amit Permalink to comment# November 18, 2016

      google transliteration Api use, but keypress use . but i have not code for keypress,please give help google api with keypress use

    • zixuan Permalink to comment# November 21, 2016

      71: g

      the alt+g: 18: alt have a alt at down and g at down.

  15. Albert Straub Permalink to comment# June 28, 2015

    How do you determine a capital letter from a lower-case letter?

    Reply
    • Carl Dolerado Permalink to comment# August 19, 2015

      You cannot tell the difference between capitals and lower case as keycode handles the key being pressed so to it there is no difference between a capital and lower case same as there is no difference between 0 and ).

    • Mottie Permalink to comment# February 28, 2016

      The keyCode will return a different value while typing in a capital letter, but only during the "keypress" event!

      Try out this demo and you’ll see that "keyup" and "keydown" always returns the capital letter keyCode and "keypress" will return the lower-case letter value.

  16. Marc Permalink to comment# August 24, 2015

    Firefox 15+ returns 173 instead of 189 for dash. jQuery’s event.which does not normalize this. Previous versions returned 109.

    Reply
  17. Marc Permalink to comment# August 24, 2015

    This list is quite good, also pointing out inconsistencies: http://www.javascripter.net/faq/keycodes.htm

    186, 187 and 189 from the above list are not safe to use.

    Reply
  18. kenny Permalink to comment# October 7, 2015

    A robust Javascript library for capturing keyboard input and key combinations entered. It has no dependencies.

    http://jaywcjlove.github.io/hotkeys/

    hotkeys('ctrl+a,ctrl+b,r,f', function(event,handler){ switch(handler.key){ case "ctrl+a":alert('you pressed ctrl+a!');break; case "ctrl+b":alert('you pressed ctrl+b!');break; case "r":alert('you pressed r!');break; case "f":alert('you pressed f!');break; } });

    hotkeys understands the following modifiers: ⇧, shift, option, ⌥, alt, ctrl, control, command, and ⌘.

    The following special keys can be used for shortcuts: backspace, tab, clear, enter, return, esc, escape, space, up, down, left, right, home, end, pageup, pagedown, del, delete and f1 through f19.

    Reply
  19. Sharkes Monken Permalink to comment# December 24, 2015

    Thank god! Great article been searching for it finally now i can identify the Keyboard codes with “KeyCode” method :) Good Job

    Reply
  20. Muhammad Yasir Permalink to comment# January 19, 2016

    Great post

    Reply
  21. Sven Permalink to comment# April 23, 2016

    Hello dear people, I mod games but I still haven’t found out what to put in an INI file for the keybinding of Arrow Up and Arrow Down. If someone can help me I would be happy to hear from you. Sven

    Reply
    • danziger Permalink to comment# November 28, 2020

      You can easily check JavaScript KeyboardEvent properties (e.key, e.code, e.which, e.keyCode… and more) with Key.js: https://keyjs.dev

      These are probably the most relevant ones for navigation in games:

      Arrow Up: 38 Arrow Left: 37 Arrow Down: 40 Arrow Right: 39 Numpad Up: 104 Numpad Left: 100 Numpad Down: 98 Numpad Right: 102 W: 87 A: 65 S: 83 D: 68

  22. Mottie Permalink to comment# May 9, 2016

    ThecharCode, keyCode and which are being deprecated. They will soon be replaced by event.key which will return named keys:

    document.querySelector('input').addEventListener('keydown', function(event) { console.log(event.key); });

    So If you type in “Hello World”, this will end up in the console:

    Shift H e l l o Shift W o r l d

    The event.shiftKey will still be true for shifted characters, but there is a new method to check for modifier keys (shift, alt, altGr, ctlr, capsLock, meta, OS, etc.) named getModifierState:

    document.querySelector('input').addEventListener('keydown', function(event) { console.log(event.getModifierState()); });

    Which will return values like this:

    Shift Alt AltGraph Control CapsLock Meta OS Reply
  23. Hrach Permalink to comment# May 15, 2016

    Is there a reason the lowest keycode is 8, or is that just how it is for no reason what so ever.

    Reply
    • Mottie Permalink to comment# May 15, 2016

      keyCodes are based on the ascii table.

  24. Mohamed Hussain S H Permalink to comment# May 16, 2016

    In Codepen shared in the article, event.which is not respecting the case, always gives me 65 when i press lowercase “a” or uppercase “A”. am I understand something wrong here?

    Reply
    • Mottie Permalink to comment# May 17, 2016

      The codes within a keypress event will provide the proper ASCII character equivalents of the typed character. Try this demo.

      Note that the keypress does not contain a value for some special keys, like the arrows, insert, delete, home, pageUp/Down, etc.

  25. Paco Permalink to comment# July 12, 2016

    I wanting ctrlKey+c = keycode?

    y try these :

    only: function(event) {

    if( event.ctrlKey && event.which === 86) { event.preventDefault(); } }

    but dont go :( … do you have another idea :)

    THX

    Reply
  26. Ramana Permalink to comment# January 15, 2017

    How to get the Ctrl+space event?

    Reply
  27. Ajin Permalink to comment# February 15, 2017

    Any one know when press 1 then the input box displays I(Uppercase of I(i))

    Reply
  28. Mottie Permalink to comment# February 15, 2017

    @chriscoyier This post will need to be updated soon… most browsers now support the new keyboard events, including getModifierState().

    http://codepen.io/Mottie/pen/bgZxod/

    Reply
  29. Paolo Permalink to comment# February 17, 2017

    I get inconsistent evt.which values when listening to keydown on Safari 9, expecially for symbols like +, -, etc..

    I get reasonable values (ascii values) if I listen to keypress.

    I need to stick with keydown for non-character keys like arrows.

    Furthermore keypress is annoying if you don’t want the event being triggered multiple times if the user holds the key down.

    Reply
  30. Subtain Permalink to comment# November 6, 2017

    Hi

    I need some help in JS code whenever I type any alphabetic word e.g. a or b then I want result 1 + 2 = 3? Is it possible, please help in this regards

    Reply
  31. Edward Gioja Permalink to comment# December 10, 2017

    Is there a way to write code that works the same on WINDOWS and MAC returns.

    e.which has different returns:

    Key Windows MAC
    0 48 186
    1 49 161
    2 50 8482
    3 51 163
    4 52 162
    5 53 8734
    6 54 167
    7 55 182
    8 56 8226
    9 57 170
    Reply
  32. Luis Permalink to comment# June 7, 2018

    Greetings. How would I do Ctrl + D on button click on most browsers? Something like this: “Favorites

    Reply
  33. RarestAiden Permalink to comment# March 3, 2019

    Hey how could i put numbers and get it to the point lets say i have a calculator i could type numbers instead of pressing them

    Reply
  34. John Perry Permalink to comment# April 13, 2019

    I could be wrong, but it looks like e.key is now the recommended approach with the widest support.

    Reply
  35. Phosphorus Permalink to comment# February 17, 2021

    Now which, charCode and keyCode are deprecated.

    Reply
  36. Noradavis Permalink to comment# June 27, 2022

    the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account

    https://www.clickercounter.org/spacebar-clicker

    Reply
  37. Asidert Permalink to comment# November 27, 2022

    The other day I was trying to think of how to put numbers and get the point across. Imagine I have a calculator and instead of pressing the buttons on the calculator, I can simply type the numbers instead.

    Reply
  38. entry type 86 Permalink to comment# August 8, 2024

    Great post! Just wanted to add that the KeyboardEvent value for entry type 86 represents a specific key event. Understanding these values can really help with advanced event handling in your apps!

    Reply
  39. Kvetnik Permalink to comment# April 21, 2025

    How do you properly detect key combinations with modifiers (e.g., Ctrl+Space or Alt+G) while accounting for differences between event.which, event.keyCode, and modern alternatives like event.key? Should you rely on getModifierState() for this, or are there more cross-browser approaches?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

Save my name, email, and website in this browser for the next time I comment.

Copy and paste this code: micuno *

Leave this field empty

Δ

Từ khóa » Bảng Keycode