![MACROMEDIA FLEX - DEVELOPING COMPONENTS AND THEMES Manual Download Page 36](http://html1.mh-extra.com/html/macromedia/flex-developing-components-and-themes/flex-developing-components-and-themes_manual_3388115036.webp)
36
Chapter 2: Creating Basic Components in Flash MX 2004
}
}
The following MXML file handles the
click
event within an
<mx:Script>
block:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*"
initialize="createListener();" >
<mx:Script>
<![CDATA[
var startAlpha:Number = 40;
var curState:Number = 100;
function myInit() {
myGS.alpha=startAlpha;
}
function modAlpha(curAlpha:Number) {
gs.alpha=curAlpha;
if (curState==100) {
curState=40;
} else {
curState=100;
}
}
]]>
</mx:Script>
<greensquare id="gs" initialize="myInit();"
click="modAlpha(curState);"
/>
</mx:Application>
For an example of a custom component that emits and handles its events, see
“Creating
compound components” on page 42
.
Handling keyboard events
Keyboard events are emitted by all components that extend the UIComponent class. To make use
of keyboard events in your MXML files, you should capture the keys and include the value of the
key in your event object, and then dispatch the keyboard event with the event object.
The following component class emits the
keyDown
event so that the Flex application can handle
it. It builds an event object, adding the ASCII value of the key that the user pressed as the
myKey
property of the event object. The class extends the UIComponent class but not the UIObject
class, which does not handle this event.
[Event("mykeydown")]
class greensquare
extends mx.core.UIComponent
{
static var symbolName:String="greensquare";
static var symbolOwner:Object = greensquare;
var className:String = "greensquare";
function greensquare() {
}