mercoledì, marzo 01, 2006

Flash TextInput Component - Focus

Have you ever wanted to open a Web page and have the focus appear on a Flash textinput field?

1. Give the Flash window focus when your HTML loads (otherwise, the focus inside the Flash movie will be irrelevant):
onload=" window.document.[swfid].focus();"

2. Disable Flash’s “Focus Manager” movieclip (see the notes section for more details) by replacing it with an empty MovieClip.

_root.createEmptyMovieClip("noFocus",focusManager.getDepth());

3. Enter the following ActionScript:

USERNAME.focusEnabled = true;
Selection.setFocus(_root.USERNAME);

Notes:

There is a bug in the “Focus Manager” for Flash when using Components.

Whenever there is a component either on the stage (or, I’ve read, in the library) there is a problem with the focus. For example, if a textinput component is on the stage, and you click it, the focus should be set to it. However, this doesn’t occur. I’ve found some great information on this bug in the Adobe’s Forums, and I’ve quoted a few bits below.

To replicate:

“…Make a new movie with a text field. Create an onEnterFrame function to trace Selection.getFocus(). When you click on the text field you'll see that it traces out the instance name of the text field. And when you click outside the text field it traces out "null". Now add a component to the stage and repeat the process. When you click the text field it traces the instance name, but when you click outside the text field it traces "null" for a split-second, then goes back to tracing the instance name.”

To fix this issue (I’m sure Adobe will revisit this in future revisions, but for now this is great):

There is a solution to this problem. Since FocusManager is just a movieclip you can replace it. I use _root.createEmptyMovieClip("noFocus",focusManager.getDepth());

Until they fix FocusManager this is the best solution.