Macromedia Flash 8 for Windows & Macintosh

The keyPress mouse event allows users to trigger actions from the keyboard by pressing a specified key. Although you assign it to a button, on (keyPress) affects the entire range of frames in which that button resides. A button symbol's Hit-frame graphic need not cover the whole Stage, and the user need not position the pointer over the button before pressing the specified key. Whenever the button appears in the currently displayed frame, pressing the specified key triggers the assigned actions.

To set up an action triggered by a key press

1.

Open a new copy of the ObjectActions-Template that you created earlier in this chapter.

2.

Select the button on the Stage. Using Script Assist or behaviors, attach a script that makes the button take you to frame 3.

(To review the required steps, see "Adding Actions to Buttons," earlier in this chapter.)

3.

Select the button on the Stage.

4.

In the Script pane of the Actions panel, select the event handler (the line of code that begins with on).

The possible events appear in the Script Assist window.

5.

Deselect the currently selected event check box (Release), and select the Key Press check box.

The text field to the right of the check box activates.

6.

In the dialog's text-entry field, type the letter a (or another letter or key that you want to act as a trigger). In the Script pane, the event handler changes to on (keyPress "a") (Figure 13.29).

Figure 13.29. Selecting the Key Press check box activates a text field where you can enter a letter (or key). Flash adds the appropriate script for using the letter you enter as a trigger for actions whenever the button appears on screen.

You're ready to test the movie. During playback, no matter where the pointer is in relation to the button, when the button is on screen and you press a on the keyboard, Flash jumps to frame 3 of the movie.

7.

Save your file for use in a later task; name the file KeyPressBtn.fla.

Tips

  • If you use a behavior to create the button script, you can also assign the triggering letter in the Behaviors panel. Select the button on the Stage. In the Event column of the Behaviors panel, double-click On Release to activate the list of events. Select On Key Press. The Keypress dialog opens, in which you can enter the triggering key (Figure 13.30). Click OK to close the dialog.

    Figure 13.30. When you choose the On Key Press event in the Behaviors panel, the Keypress dialog opens; here you can enter the triggering character (or key).

  • In some situations, assigned key-press actions fail to work in a published movie. (Browsers, for example, often intercept all key presses, assuming that the user wants to enter a new URL.) When a user clicks a button in a Flash movie, Flash grabs the key-press focusthe capability to intercept all key pressesfor itself. To ensure that your key-press actions always work, include a button for users to click before they can enter a part of the movie that uses key presses.

  • If the triggering key is also a keyboard shortcut in Test Movie mode, such as the Enter key, choose Control > Disable Keyboard Shortcuts when you test your movie. Otherwise, Flash will interpret the keypress in its normal way.

Категории