mercoledì, marzo 01, 2006

Simulating the behavior of HTML forms in Flash

I wanted to simulate the following behavior of an HTML form: on initial load, focus on the first field, then allow users to enter text and move between fields using the tab key. Like HTML, I’d like the tab key to jump fields in a particular order. Finally, I wanted the user to be able to press the “enter” key instead of pressing the enter key button with their mouse.

The Adobe Forums were quite helpful once again. Here’s the ActionScript to do it (assuming there are two input text components, named “USERNAME” and “PASSWORD” and one submit button entitled “submit_btn”).

// Set Tab Index
USERNAME.tabIndex = 1;
PASSWORD.tabIndex = 2;
submit_btn.tabIndex = 3;

// Begin SWF with USERNAME focused
USERNAME.focusEnabled = true;
Selection.setFocus(_root.USERNAME);

// Note: on an earlier frame, I also disabled Flash’s “Focus Manager” as previously described).

// If submit_btn has focus AND the user presses enter, submit the form
submit_btn.onSetFocus = function () {
t = new Object();
t.onKeyDown = function() {
if (Key.isDown(Key.ENTER)) {
getURL("http://www.mrentropy.com/bogus_submit.asp?USERNAME="+USERNAME.text+"&PASSWORD="+
PASSWORD.text+"&Submit2=Login", "_parent", "POST");
}
};
Key.addListener(t);
}

// If the submit_btn is clicked

submit_btn.onPress = function() {
getURL("http://www.mrentropy.com/bogus_submit.asp?USERNAME="+USERNAME.text+"&PASSWORD="+
PASSWORD.text+"&Submit2=Login", "_parent", "POST");
};
stop();