Home > OS >  Navigaton menu. Pressing buttons in the AS3 application without keyboard and mouse
Navigaton menu. Pressing buttons in the AS3 application without keyboard and mouse

Time:11-10

I am writing an application for Arduino on AS3. Keyboard and mouse are not provided. There are a lot of buttons in the application. All of them can work with a keyboard and mouse. Management is planned to be carried out by changing the number taken from the array. The application accepts data only from Arduino.

If Data[5] ==100, FocusManager moves to the next button. If Data[5] ==600, the button on which FocusManager is installed is clicked.

package
{
    import flash.events.*;
    import fl.managers.*;
    import flash.display.*;

    dynamic public class Main extends MovieClip
    {
        public var fm:FocusManager;
        public var myBtnFocus:InteractiveObject; 
        public var myBtnName:String = "";
        public function Main()
        {
            addFrameScript(0, this.frame1);
            
            fm = new FocusManager(this);
            fm.setFocus( this ); //# manually set your button as default //# fm.setFocus( your button );
            
            this.myBtnFocus = fm.getFocus(); //# update the reference to know currently focused
            this.myBtnName = this.myBtnFocus.name; //# extract name from focused
            return;
        }// end function

        public function Rendering() : void
        {
            this.Connect.Status.text = this.Data[5]; // view arduino Data[5]
            if (this.Data[5] > 0)
            {
                if( ! Boolean(this.chkBtn))
                {   
                    if (this.Data[5] = 100) 
                    {
                        //# SOLVED - FocusManager go to next button (component)
                        this.myBtnFocus = fm.getNextFocusManagerComponent(); //# find it as next one
                        fm.setFocus( this.myBtnFocus ); //# then set as current selection (focused)
                        this.chkBtn = true;
                    }
                    /*
                    if (this.Data[5] = 150) {FocusManager go to the previous button (component)}
                    if (this.Data[5] = 200) {move FocusManager left (Arrow left)}
                    if (this.Data[5] = 300) {move FocusManager righ(Arrow right)}
                    if (this.Data[5] = 400) {move FocusManager up(Arrow up)}
                    if (this.Data[5] = 500) {move FocusManager dwn(Arrow dwn)}
                    if (this.Data[5] = 600) {SOLVED - click on the button on which the FocusManager is installed with a mouse click }
                    */
                }
            }
            else //# if the button is pressed and Data[5]==0, deleting variables
            {
                if( Boolean(this.chkBtn)) 
                {
                    delete this.chkBtn;
                }
            }
            return;
        }// end function
        
        public function onm ouseClick (event:MouseEvent = null) :void        
        {
            if( event.currentTarget.name != null)
            { this.myBtnName = event.currentTarget.name; }
            trace( ">> Got Click ... Button name is : "   this.myBtnName );
        }
        
        public function onFocusedBtn (event:FocusEvent) :void 
        {
            trace (">> Got TAB ... Focus is now changed to : "   event.target.name)
        }
        
        function frame1()
        {
            this.Btn1.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            this.Btn2.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            this.Btn3.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            
            //# Focus listener
            this.addEventListener( FocusEvent.FOCUS_IN, onFocusedBtn );
            return;
        }// end function
    }
}

How to implement if Data[5] ==150 - FocusManager goes to the previous button? How to change the position of the FocusManager to the left, up, down, right (simulating pressing keys on the keyboard)

CodePudding user response:

To test example code below..

  • To simulate "TAB",
    send from Arduino a code 100 each time to change the selected button's index.

  • To simulate "ENTER" or "CLICK",
    send from Arduino a code 600 to know which button was clicked.

For example to simulate clicking on btn2 you send Data 3 times with: 100 --> 100 --> 600

Try something like this:

//# index of the button that was selected via keypress TAB or mouse Click
public var index_OfSelected :uint = 0;

//# for handling keyboard pressing
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

//# for where/whenever you receive data from Arduino
process_Data ( this.Data ); //run function with Arduino "Data" as function input
    
public function process_Data ( inData :Array ) :void
{
    //# we'll reference the function's input as "inData"...
    
    if (inData[5] = 100) doKey_TAB();       //# selected target my buttons (TAB)
    if (inData[5] = 200) doKey_left();      //# move target left (Arrow left)
    if (inData[5] = 300) doKey_right();     //# move target right (Arrow right)
    if (inData[5] = 400) doKey_up();        //# move target up (Arrow up)
    if (inData[5] = 500) doKey_down();      //# move target down (Arrow down)
    if (inData[5] = 600) doKey_Enter();     //# enter (mouse click)
    
    //return; //# cannot return anything if return type is ":void"
}

function keyDownHandler(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.TAB) { doKey_TAB(); }
    if (event.keyCode == Keyboard.ENTER) { doKey_Enter(); }
}

function doKey_TAB () :void
{
    //# do TAB stuff/code here...
    //# eg: highlight a selected button or menu option...
    
    index_OfSelected  ;
    
    //# reset number if pressing TAB increases higher than 15 items
    if( index_OfSelected > 15) { index_OfSelected = 1; }
    
    trace(">> Pressed TAB : current button is : Btn"   String( index_OfSelected ) );
    
}

function doKey_Enter () :void
{
    //# do Enter stuff/code here...
    trace(">> Pressed ENTER or CLICK : current button is : Btn"   String( index_OfSelected ) );
}

public function onBtn1(event:MouseEvent) : void        
{
    index_OfSelected = 1; doKey_Enter();    
}

public function onBtn2(event:MouseEvent) : void        
{
    index_OfSelected = 2; doKey_Enter();    
}

public function onBtn15(event:MouseEvent) : void        
{
    index_OfSelected = 15; doKey_Enter();   
}

function frame1()
{
    this.Btn1.addEventListener(MouseEvent.CLICK, this.onBtn1); //# index = 1
    this.Btn2.addEventListener(MouseEvent.CLICK, this.onBtn2); //# index = 2
    this.Btn15.addEventListener(MouseEvent.CLICK, this.onBtn15); //# index = 15
    
    //return; //# should Error since it does not seem to return anything...
}

CodePudding user response:

According to your shown, you can try something like below to use focusManager.

Code is untested (no AS3 compiler here).

package
{
    //# put imports here
    
    import fl.managers.FocusManager;

    dynamic public class Main extends MovieClip
    {
        //# put vars here (as public or private or static ... etc)
        
        public var fm:FocusManager;
        
        //# reference to the selected button object
        public var myBtnComponent:InteractiveObject; 
        public var myBtnName :String = "";
        
        //# /////////////////////////////////////////////////////////////
        //# Main code and supporting functions...
        //# /////////////////////////////////////////////////////////////

        public function Main()
        {
            fm = new FocusManager(this);
            addFrameScript(0, this.frame1);
            
            //# use one of these to set a default (starting) selection...
            fm.setFocus( btn1 ); //# try to manually set btn1 as default?
            fm.defaultButton( btn1 ); //# or maybe this works better?
            
            myBtnComponent = fm.getFocus(); //# update the reference to know currently focused
            myBtnName = myBtnComponent.name; //# extract name from focused
        }

        public function Rendering() : void
        {
            //# cast numbers to String for using as text
            this.Connect.Status.text = String( this.Data[5] ); // view arduino Data[5]
            
            if (this.Data[5] > 0)
            {
                if( ! Boolean(this.chkBtn))
                {   
                    
                    //# press Enter
                    if ( (this.Data[5] == 600) || (this.Data[5] == "600") )
                    {
                        //# set name manually because it's not an Event
                        //# meaning you cannot check with: event.currentTargetname
                        
                        myBtnName = myBtnComponent.name; //# extract name from focused
                        
                        doKey_Enter(); //# simulate ENTER press
                        
                    }   
                    
                    //# press TAB
                    if ( (this.Data[5] == 100) || (this.Data[5] == "100") )
                    {
                        //# TAB = go to next button (component)
                        myBtnComponent = fm.getNextFocusManagerComponent(); //# find it as next one
                        fm.setFocus( myBtnComponent ); //# then set as current selection (focused)
                        
                    }               
                    
                    //# this needs fixing ...
                    if (this.Data[5] == 200)
                    {   
                        /*
                        var event:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN);
                        event.keyCode = keyCode.LEFT;
                        dispatchEvent(event);
                        this.chkBtn = true;
                        */
                        
                        //move FocusManager left (Arrow left),then the keyboard button with the left arrow should be pressed. This will be "KEY_DOWN".                    
                        
                        //# just manually run the "onDown" keyboard function
                        onDown();
                        
                    }
                    
                    /*
                    if (this.Data[5] = 300) {move target righ(Arrow right)}
                    if (this.Data[5] = 400) {move target up(Arrow up)}
                    if (this.Data[5] = 500) {move target dwn(Arrow dwn)}
                    if (this.Data[5] = 600) {click on the button on which the FocusManager is installed with a mouse click  }
                    */
                }
            }
            else 
            {
                if( Boolean(this.chkBtn)) 
                {
                    /*
                    var event:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_UP);
                    event.keyCode = which was pressed;
                    dispatchEvent(event);
                    delete this.chkBtn;
                    */
                    
                }
            }
            
        }
        
        public function onDown (e:KeyboardEvent = null) :void 
        {
            
            var myKeyCode :int = -1; //# maybe create as public var (at top of code)
            
            if( e.keyCode != null ) 
            { myKeyCode = e.keyCode; trace("key code is : "   myKeyCode ); }
            
            //# key Left
            if ( (this.Data[5] == 200) || (myKeyCode == 37) || (myKeyCode == "LEFT") )
            {   
                //# do left key stuff or run some function...
            }
        }
        
        public function onUp (e:KeyboardEvent = null) :void 
        {
            if( e.keyCode != null ) { trace(e.keyCode   " UP"); }
        }

        public function onm ouseClick (event:MouseEvent = null) :void        
        {
            if( event.currentTarget.name != null)
            { myBtnName = event.currentTarget.name; }
            
            trace( ">> Got Click ... Button name is : "   myBtnName );
            
            fm.setFocus( myBtnComponent );
            doKey_Enter();
        }
        
        public function onFocusedBtn (event:FocusEvent) :void 
        {
            trace(">> Got TAB ... Focus is now changed to : "   event.target.name);
        }
        
        function doKey_Enter () :void
        {
            //# do Enter stuff/code here...
            
            trace(">> Got ENTER ... current Button is : "   myBtnName );
            
            if( myBtnName == "Btn1" ) { do_Btn1_stuff(); }
            if( myBtnName == "Btn2" ) { do_Btn2_stuff(); }
            
        }
        
        function do_Btn1_stuff () :void
        {
            //# code for whatever task happens 
            //# whenever your button 1 is actually clicked or Enter pressed...
            //# example: play some sound if app is like a virtual music instrument
            
            trace(">> Got RUN FUNCTION ... doing Btn1 stuff here ... " );
            
        }
        
        function do_Btn2_stuff () :void
        {
            //# code for button 2 when pressed or clicked
            trace(">> Got RUN FUNCTION ... doing Btn2 stuff here ... " );
        }
        
        function frame1()
        {
            this.addEventListener(KeyboardEvent.KEY_UP, onUp);
            this.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
            
            //Btn1.name = "Btn1"; //# if needed for updating variable: myBtnName ...
            
            Btn1.addEventListener( MouseEvent.CLICK, onm ouseClick );
            Btn1.addEventListener( FocusEvent.FOCUS_IN, onFocusedBtn );
                
            Btn2.addEventListener( MouseEvent.CLICK, onm ouseClick );
            Btn2.addEventListener( FocusEvent.FOCUS_IN, onFocusedBtn );
            
            Btn15.addEventListener( MouseEvent.CLICK, onm ouseClick );
            Btn15.addEventListener( FocusEvent.FOCUS_IN, onFocusedBtn );
        }

    } //end Class Main
    
} //end Package
  • Related