33 comments

[ 3.0 ms ] story [ 39.7 ms ] thread
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 :)
Nice, but fails for me with non-English characters on my keyboard, ğüşöçĞÜŞÖÇ, and can't differentiate i and ı.
yeah, just tested with German keyboard. I guess a localization would be needed?

Anyway, upvoting because the guys behind it are awesome.

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!

Probably relevant to the discussion, the mousetrap library: http://craig.is/killing/mice
This is my favorite js keyboard plugin by far.
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.

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.
This needs fixing. Would love to have this in my bookmarks, otherwise I'd just forget about it.
What If I told you you can click the star button on search bar to bookmark it?
Just click Bookmarks then Bookmark this page...
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

Chrome doesn't let pages capture shortcuts like ctrl+w and ctrl+t.
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
I agree! I would like to see this done.
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.

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...
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.
The best part is that keys that don't produce text input have codes on keypress that are indistinguishable from other keys.

from http://unixpapa.com/js/key.html :

> For browsers that generate keypress events for special keys, it is also generally true that event.keyCode will have the same value for "left-arrow" and "%", however we can usually tell which it is because event.which is zero for special keys (there are problems with this in Opera and Konqueror, see above). Versions of WebKit before 525 took a different approach. They invented unique values to return instead of ASCII codes for special keys, and returned the same value in event.keyCode, event.which, and event.charCode. The table below gives the extended ASCII codes returned by old WebKit versions, and also the ones returned in event.charCode on keydown and keyup events in Macintosh versions of IE.

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!
Nice! But shift-key doesn't work. For example if I want to get the code for +
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.

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

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.
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.

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?