Web server iis - 344Part IIIDocument Objects Reference} Month: Day: Year: These

344Part IIIDocument Objects Reference}

Month: Day: Year:

These three events do not fire for all keys of the typical PC keyboard on all browser versionsthat support keyboard events. The only keys that you can rely on supporting the events in allbrowsers shown in the preceding compatibility chart are the alphanumeric keys representedby ASCII values. This includes keys such as the spacebar and Enter (Return on the Mac), butit excludes all function keys, arrow keys, and other navigation keys. Modifier keys, such asShift, Ctrl (PC), Alt (PC), Command (Mac), and Option (Mac), generate some events on theirown (depending on browser and version). However, functions invoked by other key eventscan always inspect the pressed states of these modifier keys. The onkeydownevent handler works in Mozilla-based browsers only starting with Mozilla1.4 (and Netscape 7.1). Scripting keyboard events almost always entails examining which key is pressed so that someprocessing or validation can be performed on that key press. This is where the situation getsvery complex if you are writing for cross-browser implementation. In some cases, even writ- ing just for Internet Explorer gets tricky because non-alphanumeric keys generate only theonkeydownand onkeyupevents. In fact, to fully comprehend keyboard events, you need to make a distinction between key codesand character codes. Every PC keyboard key has a key code associated with it. This key code isalways the same regardless of what other keys you press at the same time. Only the alpha- numeric keys (letters, numbers, spacebar, and so on), however, generate character codes. Thecode represents the typed character produced by that key. The value might change if you pressa modifier key. For example, if you type the A key by itself, it generates a lowercase a char- acter (character code 97); if you also hold down the Shift key, that same key produces an upper- case A character (character code 65). The key code for that key (65 for Western languagekeyboards) remains the same no matter what. That brings us, then, to where these different codes are made available to scripts. In all cases, the code information is conveyed as one or two properties of the browser s eventobject. IE seventobject has only one such property keyCode. It contains key codes for onkeydownandonkeyupevents, but character codes for onkeypressevents. The NN6/Moz1 event object, onthe other hand, contains two separate properties: charCodeand keyCode. You can find moredetails and examples about these eventobject properties in Chapter 25. The bottom-line script consideration is to use either onkeydownor onkeyupevent handlerswhen you want to look for non-alphanumeric key events (for example, function keys, arrowand page navigation keys, and so on). To process characters as they appear in text boxes, usethe onkeypressevent handler. You can experiment with these events and codes in Listing15-41 as well as in examples from Chapter 25. CautionelementObject.onkeydown