Triggering Button Actions from the Keyboard

I l @ ve RuBoard

The On keyPress mouse event allows viewers 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 it resides. The button'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 is 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 in "To set up a document for testing actions," earlier in this chapter (a movie with five keyframes and a stop action in Keyframe 1).

  2. In Keyframe 1, select the button on the Stage.

  3. Access the Actions-Button panel.

  4. From the Add menu, choose Actions > Movie Control > on .

  5. In the Event section of the parameters area, do the following:

    • Uncheck the Release checkbox

    • Check the Key Press checkbox.

    • Type A in the text field (Figure 13.29).

      Figure 13.29. When you select Key Press as the Event parameter for an on action, you must specify the triggering key by typing it in the text field.

      graphics/13fig29.gif

      Flash places on (keyPress "A") and a set of curly braces in the Script pane. In the final movie, pressing the letter A on the keyboard triggers whatever actions you specify in this script.

  6. With on (keyPress "A") { (Line 1) selected in the Script pane, from the Ad menu, choose Actions > Movie Control > goto .

  7. In the parameters area, do the following:

    • Choose Go to and Stop.

    • From the Type pop-up menu, choose Frame Number.

    • Enter 2 in the Frame field.

      Flash adds gotoAndStop (2); to Line 2 in the Script pane between the curly braces (Figure 13.30).

      Figure 13.30. This script makes Flash jump to Frame 2 when the user presses A on the keyboard at a time when this button is being displayed in the movie.

      graphics/13fig30.gif

      You're ready to see the button in action.

  8. Choose Control > Test Movie.

    Flash exports the movie and opens it in Flash Player. No matter where the pointer is, when the button is on-screen and you press A on the keyboard, Flash jumps to Frame 2 of the movie.

graphics/01icon02.gif Tip

  • In some cases, assigned key-press actions fail to work in a published movie. (Browsers, for example, often intercept all key presses, as uming that the user wants to enter a new URL.) When a user clicks a button in a Flash movie, Flash sets the key-press focusthe capability to intercept all key presses. 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.


I l @ ve RuBoard


Macromedia Flash MX for Windows and Macintosh. Visual QuickStart Guide
Macromedia Flash MX 2004 for Windows and Macintosh (Visual QuickStart Guides)
ISBN: 0582851165
EAN: 2147483647
Year: 2002
Pages: 243

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net