15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

424<br />

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

Listing 29-18 (continued)<br />

<br />

<br />

<br />

Eventevent.charCodeevent.keyCode<br />

onKeyDown:<br />

<br />

onKeyPress:<br />

<br />

onKeyUp:<br />

<br />

<br />

<br />

<br />

<br />

Here are some specific tasks to try with the page to examine key codes (if you<br />

are not using a browser set for English and a Latin-based keyboard, your results<br />

may vary):<br />

1. Enter a lowercase letter “a”. Notice how the onKeyPress event handler shows<br />

the charCode to be 97, which is the Unicode (and ASCII) value for the first of<br />

the lowercase letters of the Latin alphabet. But the other two event types<br />

record just the key’s code: 65.<br />

2. Type an uppercase “A” via the Shift key. If you watch closely, you see that the<br />

Shift key, itself, generates the key code 16 for the onKeyDown and onKeyUp<br />

events. But the character key then shows the value 65 for all three events<br />

(until you release the Shift key), because the ASCII value of the uppercase<br />

letter happens to match the keyboard key code for that letter.<br />

3. Press and release the Down Arrow key (be sure the cursor still flashes in the<br />

TEXTAREA, because that’s where the keyboard events are being monitored).<br />

As a non-character key, all three events stuff a value into the keyCode property,<br />

but zero into charCode. The keyCode value for this key is 40.<br />

4. Poke around with other non-character keys. Some may produce dialog boxes<br />

or menus, but their key codes are recorded nonetheless.<br />

(NN6) eventObject.charCode

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

Saved successfully!

Ooh no, something went wrong!