|
|
Basics of user input
Introduction to capturing user inputUser 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 termsIt’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 examplesAs 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:Create an empty document using the Flash authoring tool
Select a keyframe in the Timeline.
Open the Actions panel and copy the code listing into the
Script pane.
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.
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.
Run the program using Control > Test Movie.
On
the screen, the display object is manipulated as specified in the
code.
|