Key Event HandlingBesides the mouse, the other main input device is the keyboard, at least until they get that cool computer thought-control device working. Just as with the mouse, JavaScript has the mojo to handle the keyboard. The onkeydown eventIt's handy to allow users to control your Web page via their keyboard as well as via their mouse. With the key event handlers, you can trigger events to happen when the appropriate keys are pressed. In Scripts 9.19 and 9.20, a standard slideshow (otherwise identical to the one in Script 4.17) can be viewed by pressing the left and right arrow keys on the keyboard (Figure 9.9). Script 9.19. The HTML for the slideshow.Script 9.20. Use the onkeydown handler in this script to trigger a slide change.Figure 9.9. This slideshow is controlled with keypresses, rather than mouse clicks on navigation buttons.
The onkeyup eventThe onkeyup event handler is identical to the onkeydown handler, except that (big surprise) it gets called when the user has completed pressing the key down and is now letting it come back up again. The onkeypress eventThe onkeypress event is triggered when the user both presses a key down and also lets the key back up againjust for completeness's sake. Tip
|