Thursday, November 15th, 2007>p>Jan Wolter has done the ugly work of testing keyboard event handling across platforms and browses. He has a test script that you can run yourself.
You get to read about cute variations:
If a key is held down long enough it typically auto-repeats. (Note that modifier keys never auto-repeat.) In all browsers, each auto-repeat triggers more events. In most browsers, an autorepeat is sensibly treated as a character event, but not a key event, so it triggers a keypress but not a keydown or keyup. But, of course, there is some variation
And you get the conclusion:
It’s truely impressive what an hopeless hash has been made of a simple thing
like recognizing a key pressed on the keyboard.
You’d think computer technology would have advanced far enough by now to
have this all worked out better.
The keypress events are generally the easiest to work
with. Except for some confusion with arrow keys, it’s usually easy to
identify which key was pressed. You can get the character typed by doing:String.fromCharCode(event.charCode ? event.charCode : event.keyCode);
For keydown and keyup events, you
can identify most common keys (letters, numbers, and a few others)
by just looking at the event.keyCode and more or less
pretending that it is an ASCII code.
However, it isn’t really,
a character by doing “String.fromCharCode(event.keyCode)”
are wrong. On keydown and keyup events,
the key codes are not character codes,
and this conversion will give wild results for many keys.
There is no general portable way to convert keycodes to characters.
You pretty much have to sense the browser type and base the key mapping on
Because of bugs, many keys cannot be distinguished on keydown
and keyup in Macintosh Gecko and arrow keys cannot be
distinguished on keypress events in Windows Safari.
Hope for sanity exists, with DOM3, but it has not yet arrived.