Developing Flash Lite 2.x and 3.0 Applications

Activating input text fields with ActionScript

A user can activate an input text field that has keypad focus by pressing the device's select key. A Flash Lite application can also automatically activate an input text field when it receives focus using the activateTextField command. This command activates the currently selected text field; if there is no selected field when the command is executed, nothing happens.

The most common place to call activateTextField is from within a Selection.onSetFocus handler or a TextField.onSetFocus handler. For example, suppose that your application contains two (or more) input text fields on the Stage. The following code automatically activates the text field that receives focus:

var focusListener:Object = new Object ();
focusListener.onSetFocus = function (oldFocus, newFocus) {
    // Call activateTextField:
    fscommand ("activateTextField", "");
};
TextField1.addListener (focusListener);

You can also use the TextField.prototype.onSetFocus handler to activate all text fields whenever they receive focus.

It is also possible to use keys other than the device's select key to trigger the activateTextfield command. The following code activates a text field for all number keys, which makes it easier to enter (for example) the letter "a". For example, if the application includes activateTextfield in the TextField.onSetFocus handler, the user would have to press Select and then 2; this code allows the user press 2 twice, which is more intuitive.

var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.getCode() >= 48 && Key.getCode() <= 57 ){
        fscommand("activateTextField", "");
    }
Selection.addListener (keyListener);
};

For a completed sample application that uses this technique, see the Inline Text Input sample at www.adobe.com/go/learn_flt_samples_and_tutorials. Locate the ZIP file for your version of ActionScript, download and decompress the ZIP file, and then navigate to the Samples folder to access the sample.