

Whatthekeycode.com - quickly find a js keycode for any key - timparker
http://whatthekeycode.com/

======
jayferd
Key codes are pretty good for little things, but they break down pretty
quickly if you want international support. In Mathquill, we ended up just
using a hidden textarea and a setTimeout, with a few hilarious hacks. The
comments and commit history are worth a read:
[https://github.com/mathquill/mathquill/blob/dev/src/textarea...](https://github.com/mathquill/mathquill/blob/dev/src/textarea.js)

~~~
TazeTSchnitzel
IMO, this alone is something that the W3C or WHATWG realy needs to deal with.
We have cooperation now. Cooperation on keycodes would help everyone.

~~~
colanderman
They already did: <http://www.w3.org/TR/DOM-Level-3-Events/#keys-keyvalues>

No-one listened to them.

~~~
TazeTSchnitzel
What a tragedy. :(

------
gokhan
Nice, but fails for me with non-English characters on my keyboard, ğüşöçĞÜŞÖÇ,
and can't differentiate i and ı.

~~~
kalleboo
This is just a problem with keyCodes in general. They're a very blunt tool
that would useless if it weren't that there are barely any alternatives in JS.

JavaScript has absolutely massive issues with anything regarding
internationalization. This is the language that shipped with a Y2K bug in
1995!

------
deepakg
Probably relevant to the discussion, the mousetrap library:
<http://craig.is/killing/mice>

~~~
craigc
I am the author of this library. Just wanted to point out what other people
have said. Using keycodes on there own is not very reliable across different
keyboard layouts/international keyboards.

Mousetrap defaults to trying to use character codes whenever possible, and for
keys that don't send character codes (such as arrow keys, space bar, enter,
tab, etc) it falls back to using keycodes.

So it should handle all the annoying stuff,cross browser inconsistencies, etc.
for you.

------
gibybo
Heh, I've lost count of how many times I've written "document.onkeydown =
function(e) { console.log(e.keyCode); }" to deal with this. This is probably
easier, if I can remember it when I need it :)

------
anvandare
DOM keycodes are kind of broken beyond repair[1]. They cannot be relied upon
to determine precisely which character a user entered (even jQuery's charcode
falls short here) because they do not take into account modifier keys,
compositions, or locales.

[1] For a (detailed) explanation: <http://unixpapa.com/js/key.html>

------
NVI
I made something like this a while ago <http://elv1s.ru/files/js/key-code-
detector.html>

------
kunil
On an unrelated note, is there way to forbid sites to override ctrl+w? I hate
it when a web page is not closed with a short cut.

edit: I am using firefox

~~~
moonboots
Chrome doesn't let pages capture shortcuts like ctrl+w and ctrl+t.

------
goochtek
It's kind of ironic that you can't bookmark it in Chrome using the ctrl+d
shortcut when this is one of those perfect bookmark tools.

~~~
tomaisthorpe
This needs fixing. Would love to have this in my bookmarks, otherwise I'd just
forget about it.

~~~
simontabor
Just click Bookmarks then Bookmark this page...

------
klibertp
This: [http://www.cambiaresearch.com/articles/15/javascript-char-
co...](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-
codes) was sitting in my bookmarks for quite some time now... And will be
there, because I use vrome which seems to capture the key presses when there's
no input focused :(

------
TomGullen
Nice! But shift-key doesn't work. For example if I want to get the code for +

~~~
LaRakel
Those are two keys. Key codes are not character codes. Keycodes don't change
if you switch to another keyboard layout.

Key combinations don't have separate codes.

In code you could listen for keydown on the code for shift, and then for the
code for the other key.

~~~
TomGullen
Doh! Thank you!

------
DigitalSea
Anyone ever noticed that the print screen key is from what I can see, the only
key on a conventional qwerty keyboard that doesn't have an event code? What's
the reasoning for that?

------
ernestipark
The key that is being pressed should be shown on the page somewhere - though
it would make it look uglier. But for a utility like this, probably won't
matter all that much.

~~~
talmand
<http://jsfiddle.net/talmand/pXkNz/>

Although it is interesting to tap on random buttons to see what the results
are. Some of them produce a key code but no character (SHIFT and RETURN as
examples) while others produce a different character than what I tapped.

Just tossed it together rather quickly so there's probably a better way to do
it.

------
joefarish
Doesn't work in IE8. I know IE8 isn't exactly the latest and greatest web
browser but some of us are stuck using it at work!

~~~
timparker
Should be fixed now!

------
princeverma
it would be nice if it can simultaneously display appropriate JS code to bind
for a particular key. Also, if it can recognize key combos like ctrl-alt-s

~~~
Hovertruck
What would you want it do for Ctrl+Alt+S? When you press one, it does exactly
what it should – displays the keycode for the key.

Key combos like you describe would be handled by the shiftKey/metaKey/etc
attributes of the event object provided to the event handler.

