Cross-Object Events


In HTML forms, when a user has finished entering data, he or she can click the submit button or press the Enter key while in the last text field. Well, Flash, by default, does not have the capability for users to simply press the Enter key and move on through an application or form. So how does a Flash developer overcome this dilemma? Use a Key object listener with the TextField object.

This example will create a text field that will listen for the Enter key to be pressed:

1.

Create a new Flash document.

2.

Draw an input text field on the stage.

3.

Give the text field an instance name of input_txt and make sure the border option is selected (so that you can see it on the stage).

4.

Create a new layer and name it actions.

5.

In the actions layer, open the actions panel and place these actions in it:

 //create the listener with the text field input_txt.onKeyDown = function(){ //check to make sure its the ENTER key being pressed    if(Key.isDown(Key.ENTER)){        //send a message to the output panel        trace("I detect an ENTER");    } } //add the text field as a listener Key.addListener(input_txt); 

The preceding code uses an event-handling method on a text field with a Key object event. Inside the event, it uses a conditional to check to see which key has been pressed and whether it is in fact the Enter key, and then a message is sent to the Output panel. And finally, it adds the text field as a listener to the Key object.

Test this code and you will see that not only will the event be triggered with the text field as the listener, but the focus must be in the text field itself in order for it to trigger.

That is just one of the many cross-object events that can be accomplished with listeners. Play around with some of the other objects and see if you can build your own custom events.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net