Detecting keystrokes

Just now I cut short my research to the two key properties keyCode and charCode. Of course I published the results, but I didn't go quite as far as I originally planned. The punctuation keys, especially, will remain a mystery for reasons explained on the page.

Nonetheless I produced a clear table with the meanings keyCode and charCode can have, as well as a compatibility table for the special keys (escape, home, pageDown and such). These keys will delight the hearts and minds of incompatibility-lovers from all over the world, and I heartily recommend them in case you have nothing to do. Browser vendors, especially on Mac, have made a concerted push to remain incompatible and sometimes downright vague. Of course, the lack of a public standard doesn't help.

I studied 19 keys, and only one of them (backspace) is treated exactly the same by all browsers. Fortunately most differences are pretty minor (pressing the delete key doesn't fire the keypress event in Explorer; big deal), but especially on Mac, there are serious differences, too. The Mac-only help key spans the crown: Mozilla reports keyCode 6, Safari 45, and Opera either 5 or 63, depending on the event. Go figure!

This is the blog of Peter-Paul Koch, web developer, consultant, and trainer. You can also follow him on Twitter or Mastodon.
Atom RSS

If you like this blog, why not donate a little bit of money to help me pay my bills?



Comments are closed.

1 Posted by Sebastian Werner on 30 January 2007 | Permalink

In our JavaScript framework qooxdoo ( we have tried to fix this issue, too. There is a normalization layer included since qooxdoo 0.6.3 which tries to normalize all differences between the browsers and operating systems. Maybe you want to have a look at our related blog entry here: Maybe there is something implemented we can share. Or maybe there is even something missing in our implementation. Feedback would be interesting. :)

2 Posted by pauldwaite on 31 January 2007 | Permalink

Fantastic. I looked into this a bit for a pointless little side-project a while back, and it is a bit of a mess.

I seem to remember the semi-colon key and the down arrow (or something) returned the same code.

Safari had a pretty useful proprietary property that returned a human-readable description of the key instead of the ASCII stuff. But Firefox and IE didn't, sadly.

3 Posted by Erik Arvidsson on 31 January 2007 | Permalink

Not to mention that in Gecko Win32, the keydown event repeats but on other platforms is does not, making it hard to do keyboard navigation.

The qooxdoo wrapper seems nice but come on browser vendors. Wake up!

4 Posted by Sebastian Werner on 31 January 2007 | Permalink

The problem of changing existing behavior inside the browser is that is may be break existing scripts. We really need some feature included in the browser to select a compatible version. In Microsofts Internet Explorer they have add all new features, but made them only available in Strict mode. Somebody remembers the old days where we have defined script versions inside the script tags? Maybe it would be great to reintroduce such a thing in the modern worlds again.