VI.6. Understanding Keyboard Event DataAs the W3C DOM working group discovered, keyboard events in a Unicode world are tricky things to mold into an acceptable global standard. Thus, keyboard events weren't covered in DOM Level 2 and, as of this writing, are still in Working Draft state as a Level 3 module. But keyboard events in one form or another have been implemented in browsers since Netscape 4 and IE 4. The W3C DOM Level 3 Events module Working Draft adopts some existing terminology, but also adds new syntax that keyboard event processing may adopt in the future. In the meantime, we have "old-fashioned" keyboard events, with processing that is not always straightforward due to differences in event object details across browser versions. The most important data related to a keyboard event is the identity of either the physical key being activated or the character generated by that key. These are not the same things. Every key has a numeric code associated with it. For example, a U.S. English keyboard assigns the number 65 to the key labeled A. That same key, however, can produce at least two different characters (A and a) on every U.S. computer, and even more characters on operating systems like the Macintosh (where the Option and Option-Shift modifier keys let that A key generate even more characters). It so happens that the code "65" is also the ASCII and Unicode value of the uppercase A letter (this isn't a coincidence as much as it reflects the English-centric basis of early computing). The character codes for the A and a characters are 65 and 97, respectively. For some scripting tasks, the character code is importantsuch as whether the character is a numeral, regardless of whether the user pressed a top row keyboard key or a numeric keypad key; for other tasks, the key pressed is importantsuch as whether the user pressed the PageDown key (which doesn't have a character associated with it). Complicating the issue is that the IE event object has only one property value that conveys a code for a keyboard event (event.keyCode). Mozilla, Safari, and IE/Mac have a second event object property (charCode) that conveys additional event information. To expose both the key and character codes, even with only one property, the browsers let the different keyboard events deliver different information. To read the key code, use only the keydown or keyup events; use the keypress event to read the character code. You must still reconcile the event object property differences among the browsers to work with keyboard events (although some browsers implement both approaches). The key code for the keydown and keyup events is available from the keyCode property of the event objects in IE and other browsers. For the keypress event's character code, use the keyCode property for IE, Safari, and Opera and charCode property for Mozilla browsers. Safari and IE 5/Mac provide the same character code data for both the keyCode and charCode properties of an keypress event.
To see how these different events and properties expose codes to scripts, Example VI-2 dynamically displays as many keyboard- and character-related event object properties as the browser supports (see Figure VI-2). In the course of processing each event type, the event listener functions display the keyCode properties for all browsers. If a browser also supports the charCode property, its values appear in the table. You can witness interesting characteristics of the keydown and keyup events when you use modifier keys in some browsers (IE and Opera). For example, if you type an uppercase A by pressing and holding the Shift key before typing the A key, you see that the keydown event fires, and that the keycode for the Shift key (16) appears in the keyCode property. But when you then press the A key, a new event object comes on the scene, with its own keyChar property value. Note that this is not how your scripts detect whether a modifier key is pressed during a keyboard or other event. The event object in both models has Boolean propertiesaltKey, ctrlKey, and shiftKeythat your character key event can inspect. If the keyChar property of a keydown event indicates the C key, and if the event object's ctrlKey value is TRue, the user has typed Ctrl-C. Example VI-2. Keyboard events and codes
Figure VI-2 shows some sample output for Example VI-2. Figure VI-2. Viewing keyboard event dataRegardless of the operating system you use, you should try Example VI-2 on at least two different browsers. This way you can see how the three events stuff values into event object properties in different event models. |