The aim of the tutorial is to learn how to set
the focus on a Flash Movie so that when a web page first loads the cursor
is in the first Input Box. This saves the user the hassle of having to click
on the flash movie or in the input box before they can start typing. This
tutorial will also show you how to control the Tab Key so that the cursor
moves to the next input box of your choice.
(The button below only works in: Microsoft
Internet Explorer!)
Example of setting focus on a flash
movie when a web page first loads
Step One: Create the Input Box
Open a new Flash Movie.
Select the Text tool and drag a text box on stage.
Note: If you have dragged the text tool
you should get a paragraph text box which has a square in the top right
hand corner:
Open the Property Inspector: Window > Properties(Ctrl
F3)
With your Text Box still highlighted select: Input
Text(from the drop down menu):
Select the font, font style, size and colour.
Select: Left Justify.
If your Property Inspector is in collapsed view, click on
the down arrow in
the bottom right corner of the Property Inspector to expand the panel. If
you have an up arrow your panel is already expanded: Expand Property Inspector
In the Property Inspector give your Text Box the Instance
Name: /:Text1
In the Property Inspector give your Text Box the Var
Name:/:Text1
Text Box Properties.
Create a new: Text Box
Note: The easiest way is to copy and paste the existing
Text box.
Give this new Text Box the Instance Name and Var
Name: /:Text2
Create a third Text box with the Instance Name and Var Name: /:Text3
My Three Input Boxes.
Add 3 static lines of text next to each of the above Text Boxes: Input
1: Input 2: Input 3:
My Three input Text Boxes now have labels: Input 1 to 3.
Place a button Off Stage: Button
Note: This button is there so that we can attach some
ActionScript to it. It is not designed to be seen (or
clicked!!). For this reason you can use any standard button that
you may find in the Button Library: Window > Other Panels > Common
Libraries > Buttons
Do not use Knobs, Faders or Component buttons as they have different ActionScript.
Add this code to your button:
on (keyPress"<Tab>"){ if (_root.a
== 0) { Selection.setFocus("_root.Text1"); Selection.setSelection(length(/:Text1),
length(/:Text1)); _root.a
= 1;
}else if (_root.a
== 1) { Selection.setFocus("_root.Text2"); Selection.setSelection(length(/:Text2), length(/:Text2)); _root.a
= 2;
}else if (_root.a
== 2) { Selection.setFocus("_root.Text3"); Selection.setSelection(length(/:Text3), length(/:Text3)); _root.a
= 0;
}
}
Add a new layer and call it ActionScript
Add this code to the first frame of the ActionScript:
var a = 0;
Step Two: Publishing the Movie
Save your file: File > Save (Ctrl S)
Go to: File > Publish Settings (Ctrl
Shift F12)
For Type select only: Flash
Click: Publish
Click:OK
Note: This will create a Flash swf (Shock Wave File)
in the same location as your main Flash Movie. You will now have two files
such as:
My Flash files.
Take a note of the Width and Height of
your Movie. You can find this info in: Modify > Document(Ctrl
J)
Save your file: File > Save (Ctrl
S)
Close Flash: File > Exit (Ctrl Q)
Open your favourite: Web Editor
Note: You can use any program that will enable you to
create web pages and edit the HTML such as Dreamweaver, FrontPage or Note
Pad.
Open or create a standard: HTML Web page
Save this file.
Note: This web page and the Flash Movie (swf file) must
be saved to the same location or folder. You may need to use My Computer
or some similar file management program to move the Flash swf file to the
same location as the HTML web page.
Add the Flash Movie to your web page by using this HTML code:
Change the XXX to the values for the width
and height of your movie.
Change the NameOfYourSWFMovie.swf to the
name of your: Flash swf file
Important: You do not need to change the: id=mymovie
But if you do wish to change this Do Not use capital letters
in the id as this can cause problems with some browsers.
In the opening Body Tag in your HTML code change to:
<body onLoad="window.document.mymovie.focus();">
That's it !
When you run the movie and click in the first text box and
press the Tab key a few times your notice that it moves from box to box.
The advantage of this code is that if an input box has something in it, it
will set the cursor at the end of the text which is very handy. Try typing
something short into one of the input box's when you run the movie and then
press the Tab key until that input box has focus again. You will notice how
the blinking cursor is set at the end of the length of text that you typed
in.
Important Note: When you view
the Flash Movie in Internet Explorer the Movie will automatically have
focus when the page loads and set focus will be on the first input box.
You will see the blinking cursor automatically displayed and
you can start typing without having to click on the Flash Movie
first. HOWEVER! There is currently no known method to set
focus on a Flash Movie when the web page first loads and that works in
ALL Browsers. This is the best method to date but it ONLY works in Microsoft’s
Internet Explorer and NOT Netscape or Mozilla etc. For any other
Browser you have to select the Flash Movie before it has focus which
is a unavoidable for now. We can only hope that one day soon
Macromedia or Internet Browser's companies decide
to make life easier for us all. They could formulate a method
to set focus on a Flash Movie when a web page first loads that works
on ALL known browsers. Also remember that if you use the swf on it’s
own (say your making a web site entirely out of flash) that
the swf will not automatically have focus and that the user will still
have to click on it to set the focus.
(The button below only works in Microsoft’s Internet
Explorer!)
Open a new Flash Movie: File > New
Next select the Text tool:
On the Stage drag out a: Text Box
Note: If you have
dragged the Text Tool you should get a paragraph text box which has
a square in the top right hand corner:
Paragraph Text Box.
Open the Property Inspector: Window> Properties(Ctrl
F3)
With the Text Box still highlighted select: Input
Text
Selecting Input Text from
the Property Inspector.
Select any Font, Style, Size and Colour:
Selecting the Font Style.
Select: Left Justify.
If your Property Inspector is in collapsed view, click on
the down arrow in
the bottom right corner of the Property Inspector to expand the panel. If
you have an up arrow your panel is already expanded: Expand Property Inspector
In the Property Inspector give the Text Box the Instance
Name: inTxt_1
In the Property Inspector give the Text Box the Var Name: inTxt_1
Properties for the Text Box.
Create a new Text Box and call
the Instance Name and Var Name: inTxt_2
Create a new Text Box and call the Instance Name and Var Name:
inTxt_3
Create a new Text Box and call the Instance Name and Var Name: tf_4
Note: This is named differently to the rest: tf_4
Create a new Text Box and call the Instance Name and Var Name: Txt_5
Create a new Text Box and call the Instance Name and
Var Name:inTxt_6
Create a new Text Box and call the Instance Name and Var Name: inTxt_7
Now add Static Text next to each input field:
Text
Input 1:
Text Input 2:
Text Input 3:
Text Input 4:
Text Input 5:
Text Input 6:
Text
Input 7:
My Stage layout with the Input Boxes and their labels.
Select the first three: Input Boxes
Right click (Mac: Ctrl click) and
select: Convert to Symbol
For name type: mc1
For behaviour select: Movie Clip
In the Property Inspector give this new Movie Clip an Instance
Name: mc1
Giving the Symbol an Instance name.
Now select the last three input box's and convert them to
a: Movie Clip
Give this new Symbol the Instance Name: mc2
Note: You
should have one input box in the middle that's not a movie clip (the
forth one down).
Add a new layer and call it: ActionScript
Add this code to that layer in the first frame it:
//set mc
container properties to enable tabbing through child objects
mc1.tabChildren = true;
mc1.tabEnabled = false;
mc2.tabChildren = true;
mc2.tabEnabled = false;
Again this has the same Browser compatibility problems as
the first movie but is just a good example of how to set up and use the focus
manger in Flash