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 normalizesevent.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
orcharCode
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 theShift
key is held down). Otherwise, the code of the pressed key is stored inkeyCode
.
Tester Tool
Keycode values
Here’s a table that contains the values from event.which
.
|
|
|
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:
Key | event.which in keydown | event.which in keypress |
a | 65 | 97 |
b | 66 | 98 |
c | 67 | 99 |
It is recommended by jQuery to use
e.which
overe.keyCode
, as it gets normalised across all browsers.It also takes into account
mouseup
andmousedown
.Not trying to be funny, just trying to help!!
http://api.jquery.com/event.which/
By mouseus, I clearly meant mouseup!
I agree with this, I stopped using
e.keyCode
along time ago when I found oute.which
was more widely supported.What about JQuery Hotkeys plugin?
https://github.com/tzuryby/jquery.hotkeys
Great tip Chris, thanks for sharing !
I never figured it is that simple to know keyCodes with jQuery.
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
Dang, im still using this:
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!
var code = e.keyCode || e.which; //is a shorter/cleaner version of what you have
@spyter: just know that if
e.keyCode === 0
, then it will be falsey and fall through toe.which
(which may not be a problem, but is a possibility)hahahah very smart
I found that the Apple command key is 224, which is not on this list.
only on firefox in chrome it is 93
sorry meant 91
The left command key is 91 and the right command key 93. So in your code please check for both.
i want alt+q =keycode??
To detect that you’d need to look at the
altKey
value:i want alt+g =keycode??
@chriscoyier The table seems to be missing “space” with a
event.which
value of 32 :)What about the key codes for other languages?
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.
You are using
element.onkeypress
, to get the arrow keys you useelement.onkeydown
.Thank you for exact copy paste and don’t read even once !
I mean: ‘close braket’ just for fun ;)
Please also add these:
NUM_LOCK_MAC: 12,
F13: 124,
F14: 125,
F15: 126,
F16: 127,
F17: 128,
F18: 129,
F19: 130,
I wanting alt+q = keycode?
There are some conflicting keycodes which belong to two characters when using keyup or keydown:
188 , <
190 . >
191 / ?
192 ` ~
219 [ {
220 \ |
221 ] }
222 ‘ “
I have tried following but not working
this will help you i think
google transliteration Api use, but keypress use . but i have not code for keypress,please give help google api with keypress use
71: g
the alt+g:
18: alt
have a alt at down and g at down.
How do you determine a capital letter from a lower-case letter?
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 ).
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 letterkeyCode
and"keypress"
will return the lower-case letter value.Firefox 15+ returns 173 instead of 189 for dash. jQuery’s event.which does not normalize this. Previous versions returned 109.
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.
A robust Javascript library for capturing keyboard input and key combinations entered. It has no dependencies.
http://jaywcjlove.github.io/hotkeys/
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
andf1
throughf19
.Thank god! Great article been searching for it finally now i can identify the Keyboard codes with “KeyCode” method :) Good Job
Great post
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
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
The
charCode
,keyCode
andwhich
are being deprecated. They will soon be replaced byevent.key
which will return named keys:So If you type in “Hello World”, this will end up in the console:
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.) namedgetModifierState
:Which will return values like this:
Is there a reason the lowest keycode is 8, or is that just how it is for no reason what so ever.
keyCodes are based on the ascii table.
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?
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.I wanting ctrlKey+c = keycode?
y try these :
only: function(event) {
but dont go :( … do you have another idea :)
THX
How to get the Ctrl+space event?
Any one know when press 1 then the input box displays I(Uppercase of I(i))
@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/
I get inconsistent
evt.which
values when listening tokeydown
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.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
Is there a way to write code that works the same on WINDOWS and MAC returns.
e.which has different returns:
Greetings.
How would I do Ctrl + D on button click on most browsers?
Something like this:
“Favorites
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
I could be wrong, but it looks like
e.key
is now the recommended approach with the widest support.Now which, charCode and keyCode are deprecated.
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
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.
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!
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?