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: <a href="https://github.com/mathquill/mathquill/blob/dev/src/textarea.js" rel="nofollow">https://github.com/mathquill/mathquill/blob/dev/src/textarea...</a>
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 :)
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.<p>[1] For a (detailed) explanation: <a href="http://unixpapa.com/js/key.html" rel="nofollow">http://unixpapa.com/js/key.html</a>
I made something like this a while ago <a href="http://elv1s.ru/files/js/key-code-detector.html" rel="nofollow">http://elv1s.ru/files/js/key-code-detector.html</a>
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.<p>edit: I am using firefox
This: <a href="http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes" rel="nofollow">http://www.cambiaresearch.com/articles/15/javascript-char-co...</a> 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 :(
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?
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.
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