User Guide
About ActionScript components 123
Example: Creating a simple component
When you define a simple component, you do not create a component yourself, but you
modify the behavior of an existing component. In this section, you create a customized
TextArea control by extending the mx.controls.TextArea component. This component adds
an event listener for the
keyDown event to the TextArea control. The KeyDown event deletes all
the text in the control when a user presses the Control+Z key combination:
package myComponents
{
// as/myComponents/DeleteTextArea.as
import mx.controls.TextArea;
import flash.events.KeyboardEvent;
public class DeleteTextArea extends TextArea {
// Constructor
public function DeleteTextArea() {
// Call super().
super();
// Add event listener for keyDown event.
addEventListener("keyDown", myKeyDown);
}
// Define private keyDown event handler.
private function myKeyDown(eventObj:KeyboardEvent):void {
// Check to see if Ctrl-Z pressed. Keycode for Z is 90.
if (eventObj.ctrlKey && eventObj.keyCode == 90)
text = "";
}
}
}
The filename for this component is DeleteTextArea.as, and its location is in the
myComponents subdirectory of the application, as specified by the
package statement. For
more information on using the
package statement, and specifying the directory location of
your components, see Chapter 3, “Using ActionScript to Create Components,” on page 25.
You can now use your new TextArea control in an application, as the following example
shows:
<?xml version="1.0"?>
<!-- as/MainDeleteTextArea.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myComponents.*">
<MyComp:DeleteTextArea/>
</mx:Application>