15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

112<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

elementObject.onKeyDown<br />

onKeyDown, onKeyPress, and onKeyUp events. If you hold down one or more modifier<br />

keys while performing the key press, the modifier key name is highlighted for<br />

each of the three events. Note that when run in NN6, the keyCode value is not the<br />

character code (which doesn’t show up in this example for NN6). Also, you may need<br />

to click the NN6 page for the document object to recognize the keyboard events.<br />

The best way to watch what goes on during keyboard events is to press and hold<br />

a key to see the key codes for the onKeyDown and onKeyPress events (see Figure<br />

1-5). Then release the key to see the code for the onKeyUp event. Notice, for<br />

instance, that if you press the A key without any modifier key, the onKeyDown event<br />

key code is 65 (A) but the onKeyPress key code in IE (and the charCode property<br />

in NN6 if it were displayed here) is 97 (a). If you then repeat the exercise but hold<br />

the Shift key down, all three events generate the 65 (A) key code (and the Shift modifier<br />

labels are highlighted). Releasing the Shift key causes the onKeyUp event to<br />

show the key code for the Shift key.<br />

Figure 1-5: Pressing Ctrl+Alt+J in the keyboard event lab page<br />

In another experiment, press any of the four arrow keys. No key code is passed<br />

for the onKeyPress event because those keys don’t generate those events. They<br />

do, however, generate onKeyDown and onKeyUp events.<br />

Listing 15-41: Keyboard Event Handler Laboratory<br />

<br />

<br />

Keyboard Event Handler Lab<br />

<br />

TD {text-align:center}<br />

<br />

<br />

function init() {<br />

document.onkeydown = showKeyDown<br />

document.onkeyup = showKeyUp

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!