O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Focus Events

When an object has keyboard focus, it acts as the logical recipient of all keyboard input and becomes the target of all keyboard-input event dispatches. An object can gain keyboard focus either programmatically (via the Stage class's instance variable focus) or through user interaction, via the mouse, the Tab key, or the arrow keys. However, in order to gain keyboard focus, an object must be an instance of a class that inherits from InteractiveObject. Furthermore, in Flash Player, only one object can have keyboard focus at a time.

Tip

For brevity, the term "keyboard focus" is normally shortened to "focus."

Focusing Objects Programmatically

To focus an object programmatically, we assign that object to the Stage instance's focus variable.

For example, the following code creates a Sprite object and then immediately focuses it (it assumes that someDisplayContainer is on the display list):

var rect:Sprite = new Sprite();
rect.graphics.lineStyle(1);
rect.graphics.beginFill(0x0000FF);
rect.graphics.drawRect(0, 0, 150, 75);
someDisplayContainer.addChild(rect);
someDisplayContainer.stage.focus = rect;

When the preceding code runs, rect gains focus, and, hence, becomes the target of all keyboard input event dispatches.

Focusing Objects with the Keyboard

To focus an object via the keyboard, the user presses the Tab key or arrow keys. However, in order for an object to receive focus via those keys, it must be part of Flash Player's tab order. The tab order is the set of all objects on the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required