Search code examples
actionscript-3actionscriptflash-cs4flash-cs5

Basic AS 3.0 query


What is the best way to navigate through an actionscript swf using arrows?


Solution

  • set the tabIndex property of the TextInput. That should allow you to tab through the form.

    It is inadvisable to override the default functionality for arrow keys because they are used to move the text insertion point within the textInput

    As for enter, you'll have to listen for the keyUp event and, if you detect an enter key, move to the next field.

    //add this eventlistener for each textbox (through a loop or manually)
    t.addEventListener(KeyboardEvent.KEY_UP, k);
    
    //The event handler
    protected function k(e:KeyboardEvent):void {
        if(e.keyCode==Keyboard.ENTER) {
             focusManager.getNextFocusManagerComponent().setFocus();
        }
    }
    

    EDIT

    For Flash CS5, this code should work:

    import flash.events.KeyboardEvent;
    import fl.managers.FocusManager; 
    import flash.display.InteractiveObject; 
    import fl.managers.IFocusManagerComponent;
    import fl.managers.IFocusManager;
    
    t1.addEventListener(KeyboardEvent.KEY_UP, k);
    t1.tabIndex=1;
    t2.tabIndex=2;
    
    var fm:FocusManager=new FocusManager(this);
    
    t1.tabEnabled=true;
    t2.tabEnabled=true;
    
    function k(e:KeyboardEvent):void {
        if(e.keyCode==Keyboard.ENTER) {
             var fx:InteractiveObject = fm.getNextFocusManagerComponent();
             fm.setFocus(fx);
        }
    }
    

    important: first drag a component from the "User Interface" group onto the stage and delete it. This should put all the required components in the library ready for you to use

    EDIT2

    Change

    for(var i:int=0; i < textbox.length; i++) {
            //textbox[i].buttonMode = true;
            //box[i].addEventListener(MouseEvent.CLICK, myclick_ftn);
            //box[i].addEventListener(FocusEvent.FOCUS_IN,textInputHandler);
            //box[i].addEventListener(FocusEvent.FOCUS_OUT,textInputHandlerOut);
            textbox[i].restrict = "0-9";
            textbox[i].addEventListener(KeyboardEvent.KEY_UP, k);
            textbox[i].tabIndex=i;
            //t2.tabIndex=2;
    
            //textbox[i].tabEnabled=true;
    
                    var fm:FocusManager=new FocusManager(this);
    
    
            function k(e:KeyboardEvent):void {
                if(e.keyCode==Keyboard.ENTER) {
                    var fx:InteractiveObject = fm.getNextFocusManagerComponent();
                    fm.setFocus(fx);
        }
    }
            //t2.tabEnabled=true;
    }
    

    in your code to this:

    var fm:FocusManager=new FocusManager(this);
    
    
    function k(e:KeyboardEvent):void {
        if(e.keyCode==Keyboard.ENTER) {
            var fx:InteractiveObject = fm.getNextFocusManagerComponent();
            fm.setFocus(fx);
        }
    }
    
    for(var i:int=0; i < textbox.length; i++) {
            //textbox[i].buttonMode = true;
            //box[i].addEventListener(MouseEvent.CLICK, myclick_ftn);
            //box[i].addEventListener(FocusEvent.FOCUS_IN,textInputHandler);
            //box[i].addEventListener(FocusEvent.FOCUS_OUT,textInputHandlerOut);
            textbox[i].restrict = "0-9";
            textbox[i].addEventListener(KeyboardEvent.KEY_UP, k);
            textbox[i].tabIndex=i;
            //t2.tabIndex=2;
    
            //textbox[i].tabEnabled=true; 
    
    }