One of the main goals of any good application is to make the user's experience as pleasant and straightforward as possible. A large part of accomplishing this goal involves anticipating the user's needs and making sure that your application allows users to accomplish tasks with minimal effort. For example, look at the process of doing a search on Google. When you navigate to Google's homepage, your cursor is automatically inserted in the Search box because they believe that your main purpose for visiting their site is to do a search. It's a simple thing, but it allows you to begin typing keywords as soon as the page loads, without having to first drag your mouse to place the cursor in the Search box manually.
What happens when you press Enter/Return? Because you've typed keywords into the Search box, pressing Enter/Return should submit your keywords to Google's search engine. Fortunately, it does. Imagine how awful it would be if pressing Enter/Return took you to their Help page. This would make using Google a real pain.
These kinds of simple anticipatory behaviors within an application are deliberately programmed by the developer as a way of making the application easier to use. Besides Google's homepage, you've probably experienced similar functionality in other applications especially those with dialog boxes where pressing the Tab key allows you to quickly navigate from one field to the next.
Within Flash, these types of interactions are handled by the FocusManager component. Why is this component special? Because you never see it physically, unlike Button or CheckBox component instances. You can only see its effects an invisible instance of the FocusManager component is automatically added to your project whenever you add a regular component instance. The FocusManager component takes care of managing focus-related tasks via ActionScript.
Because the FocusManager component is invisible, it can't be configured using the Component Inspector or Property Inspector, as other component instances can. It can only be configured and worked with by using ActionScript.
The term focus is simply a computer geek term meaning the object that's currently being manipulated. Type text into TextInput component instance, and it's said to have focus. Click a button; it has focus. Select a check box, and it has focus. Only a single item at a time in an application can have focus.
As you can see, what's considered as having focus in an application is constantly in flux. Normally, the user controls what has focus at any particular time due to the way he or she is interacting with the application. However, the FocusManager component gives you some control over the focus. Why would having control over the focus aspects of your application be helpful? Consider these scenarios.
The user clicks a check box in your application, indicating that he or she wants to receive email about special offers. The FocusManager component allows you to automatically give focus to the TextInput instance, allowing the user to immediately begin typing an email address after selecting the check box without having to manually move the cursor to the textbox. If the TextInput instance is named myTextInput_ti, it would be given focus using the following syntax:
You want users to be able to press Enter/Return at any time, depending on the task they're trying to accomplish, and have Enter/Return simulate clicking the appropriate button. Because our sample application allows the user to enter an email address, chances are that there's a button on the interface that, when clicked, will submit the address. Instead of making the user click the button, you can set the FocusManager's setDefaultButton property like this:
focusManager.setDefaultButton = mySubmitButton_pb;
As a result, the click event for the Button component instance mySubmitButton_pb will be triggered when the Enter/Return button on the keyboard is pressed. Any objects registered to listen for this event will be notified that it has occurred, thus executing any scripts set up to execute as a result of the event.
In the following exercise, we'll use the functionality the FocusManager provides to make our application a bit more user-friendly in several ways, which we'll explain as we go along.