Key Events
Strategy: normalize to Mozilla as much as possible
The onkeypress event appears to return the most reliable keyboard data in general. IE does not fire keypress for most non-printable keys so we patch IE to simulate keypress events.
Rule: rely on keypress
keyCode for non-printable keys can vary by browser and platform. Use dojo.keys constants to test for a specific keyCode instead of hardcoded values.
Rule: use dojo.keys
We normalize to the Mozilla convention where charCode has a value for printable keys, and keyCode has a value for non-printable keys. We also introduce a proprietary String property keyChar, which is the string corresponding to charCode (or '').
Normalizations
- All
- String property keyChar assigned to the string matching the key pressed, or '' for non-printable
- IE
- keypress events are simulated for keydown events that would otherwise omit them
- ASCII keyChar values are generated for CTRL-NUMPAD[0-9] key codes
- keyChar values are lowercased for CTRL-[A-Z]
- ASCII keyChar values are generated for CTRL-[,./[]\;'/*], but these are generally US-keyboard specific
- some keyboard events can only be prevented by setting keyCode to 0 (vs setting returnValue to false), so we include this assignment in the preventDefault() assigned to normalized events
- CTRL-BREAK is morphed into CTRL-c to match Mozilla
- Opera
- keyChar values are lowercased for CTRL-[A-Z]
- CTRL-BREAK is morphed into CTRL-c to match Mozilla
- Safari
- entirely replace the Safari event with a synthetic one to be able to control shiftKey, really for the sole purpose of trapping SHIFT-TAB, which has a special code on Safari. This also allows us to normalize charCode which is otherwise read-only.
- the (DOM3) keyIdentifier property is used to differentiate ENTER from CTRL-m (both code 13)
- keyChar values are lowercased for CTRL-[A-Z]
Testing Notes
- Safari testing done on Mac OS X 10.4.9
- Opera testing done on Opera 9.20.8771 on Windows Vista
- IE testing done on IE 7.0.6000.16386 on Windows Vista
- IE testing done on IE 6.0.2900.2180 on Windows 2000 (2600.xpsp_sp2_gdr.050301-1519)
- Mozilla testing done on FireFox 2.0.0.3 on Windows Vista
- Konqueror testing done on Konqueror 3.4.0-5 Red Hat (Using KDE 3.4.0-6 Red Hat)
General Notes
- ALTGR key available in some locales can generate events indistinguishable from CTRL keys on US keyboards
- keypress repeats generally only occur after a slight delay, realtime applications like games might require monitoring keydown/keyup
- Safari
- appears to support the robust DOM-3 keyboard spec
- doesn't appear possible to suppress certain ALT or CTRL combinations
- keyCode and charCode are read-only
- Mozilla
- doesn't appear possible to prevent CTRL-PgUp/PgDn
- doesn't differentiate between numpad numbers (when NUMLOCK is on) and the standard number keys
- produces lower-case codes for CTRL-[a-z]
- IE
- doesn't appear possible to prevent F1 from opening Help without using <body onhelp="return false">
- doesn't appear possible to suppress ALT-combinations or CTRL-+/-
- CTRL in combination with punctuation keys yields only virtual key codes, currently we map these to US equivalents
- keys can trigger Windows system hooks which are not preventable from the browser
- Opera
- doesn't appear possible to capture most ALT-combinations, except for ALT-<number> and ALT-<F[n]>
- doesn't appear possible to suppress ALT-combinations
- doesn't appear possible to detect DELETE and INSERT, as they produce identical event information as "." and "_"
- Konqueror
- Konqueror (as tested) is broken because all keyboard events have type == "keydown"