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:
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. |