Basics of user input



Introduction to capturing user input

User interaction, whether by keyboard, mouse, camera, or a combination of these devices, is the foundation of interactivity. In ActionScript 3.0, identifying and responding to user interaction primarily involves listening to events.

The InteractiveObject class, a subclass of the DisplayObject class, provides the common structure of events and functionality necessary for handling user interaction. You do not directly create an instance of the InteractiveObject class. Instead, display objects such as SimpleButton, Sprite, TextField, and various Flash authoring tool and Flex components inherit their user interaction model from this class and therefore share a common structure. This means that the techniques you learn and the code you write to handle user interaction in an object derived from InteractiveObject are applicable to all the others.

The following typical user interaction tasks are described in this chapter:

  • Capturing application-wide keyboard input

  • Capturing keyboard input to a specific display object

  • Capturing application-wide mouse actions

  • Capturing mouse input to a specific display object

  • Creating drag-and-drop interactivity

  • Creating a custom mouse cursor (mouse pointer)

  • Adding new behaviors to the context menu

  • Managing focus

Important concepts and terms

It’s important to familiarize yourself with the following key user interaction terms before proceeding:

  • Character code: A numeric code representing a character in the current character set (associated with a key being pressed on the keyboard). For example, “D” and “d” have different character codes even though they’re created by the same key on a U.S. English keyboard.

  • Context menu: The menu that appears when a user right-clicks or uses a particular keyboard-mouse combination. Context menu commands typically apply directly to what has been clicked. For example, a context menu for an image may contain a command to show the image in a separate window and a command to download it.

  • Focus: The indication that a selected element is active and that it is the target of keyboard or mouse interaction.

  • Key code: A numeric code corresponding to a physical key on the keyboard.

Working through in-chapter examples

As you’re working through the chapter, you may want to test some of the sample code listings for yourself. Because this chapter is about working with user input in ActionScript, essentially all the code listings in this chapter involve manipulating some type of display object—usually a text field or any InteractiveObject subclass. For the purposes of the examples, the display object either can be one that has been created and placed on the Stage in Adobe® Flash® CS4 Professional, or can be one that’s created using ActionScript. Testing a sample involves viewing the result in Flash Player or Adobe® AIR™ and interacting with the sample to see the effects of the code.

To test the code listings in this chapter:

  1. Create an empty document using the Flash authoring tool

  2. Select a keyframe in the Timeline.

  3. Open the Actions panel and copy the code listing into the Script pane.

  4. Create an instance on the Stage:

    • If the code refers to a text field, use the Text tool to create a dynamic text field on the Stage.

    • Otherwise, create a button or movie clip symbol instance on the Stage.

  5. Select the text field, button, or movie clip, and give it an instance name in the Property inspector. The name should match the name of the display object in the sample code—for example, if the code manipulates an object named myDisplayObject, name your Stage object myDisplayObject as well.

  6. Run the program using Control > Test Movie.

    On the screen, the display object is manipulated as specified in the code.