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

Preventing Default Event Behavior

Some events in ActionScript are associated with a side effect known as a default behavior. For example, the default behavior of a TextEvent.TEXT_INPUT event is text being added to the target text field. Likewise, the default behavior for a MouseEvent.MOUSE_DOWN event targeted at a SimpleButton object displays the button's "down state" graphic.

In some cases, events with a default behavior offer the option to prevent that behavior programmatically. Events with a default behavior that can be prevented are said to be cancelable. For example, the TextEvent.TEXT_INPUT event is cancelable, as are FocusEvent.KEY_FOCUS_CHANGE and FocusEvent.MOUSE_FOCUS_CHANGE.

To prevent the default behavior for a cancelable event, we invoke the Event class's instance method preventDefault( ) on the Event object passed to any listener registered for that event. For example, in the following code, we prevent the default behavior for all TextEvent.TEXT_INPUT events targeted at the text field t. Instead of allowing the user-entered text to appear in the text field, we simply add the letter "x" to the text field.

package { import flash.display.*; import flash.text.*; import flash.events.*; // Changes all user-entered text to the character "x" public class InputConverter extends Sprite { private var t:TextField; public function InputConverter () { // Create the text field t = new TextField(); t.border = true; t.background = true; t.type = TextFieldType.INPUT addChild(t); // Register ...

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