R – AIR application, set cursor position in form

actionscript-3airapache-flexcursor

I have an AIR application with a login form. What I want to do is set the cursor in the first textinput box. I only manage to set the focus on the box, but not the cursor.

Does anyone have an idea for how I can do this?

Best Answer

You need to wait for the flex container to be registered with the display list so you access the stage.

Put a call to init from you creationComplete handler:

<mx:Script>
    <![CDATA[
        import flash.events.Event;

        private function init():void 
        {
            addEventListener(Event.ADDED_TO_STAGE, initScreen, false);

        }

        private function initScreen(e:Event):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, initScreen);
            stage.focus = userName;
        }

    ]]>
</mx:Script>

<mx:Form defaultButton="{enterBtn}">

    <mx:FormHeading label="Form" />
    <mx:FormItem label="Username" tabIndex="1">
        <mx:TextInput id="userName" text="" selectionBeginIndex="0" />
    </mx:FormItem>
    <mx:FormItem label="Password" tabIndex="2">
        <mx:TextInput displayAsPassword="true" id="password"/>
    </mx:FormItem>

</mx:Form>
Related Topic