Home > database >  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:04-11

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 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] = 200, then the keyboard button with the left arrow should be pressed. This will be "KEY_DOWN". If Data[5] = 0, then it will be "KEY_UP". for example:

dynamic public class Main extends MovieClip
{
    public function Main()
    {
        addFrameScript(0, this.frame1);
        return;
    }// end function

    public function myFunctions() : void
    {
        //myCode
        if (this.Data[5] = 100) {selected target my buttons (TAB)}
        if (this.Data[5] = 200) {move target left (Arrow left)}
        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) {mouse click on the selected button }
        return;
    }

    public function onBtn1(event:MouseEvent) : void     
    {
        trace("pessed btn1")
        return;
    }// end function    
    
    public function onBtn2(event:MouseEvent) : void     
    {
        trace("pessed btn2")
        return;
    }// end function    
    
    public function onBtn15(event:MouseEvent) : void        
    {
        trace("pessed btn15")
        return;
    }// end function    

    
    function frame1()
    {
        this.Btn1.addEventListener(MouseEvent.CLICK, this.onBtn1);
        this.Btn2.addEventListener(MouseEvent.CLICK, this.onBtn2);
        this.Btn15.addEventListener(MouseEvent.CLICK, this.onBtn15);
        return;
    }// end function

}

How to make an imitation of pressing a button from the value of a variable, please help.

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:

Thanks a lot, the code works fine.How to implement a real keystroke? Here is my code, I tried to implement pressing TAB. Everything works in the console, but I do not know how to send a command, as with the keyboard.

{
    public var Data:Array;
    public function Main()
    {
        addFrameScript(0, this.frame1);
        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)
                {
                    var event:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN);
                    event.keyCode = 9;
                    //event.keyCode = Keyboard.TAB;
                    dispatchEvent(event);
                    this.chkBtn = true;
                }
            }
        }
        else 
        {
            if( Boolean(this.chkBtn)) 
            {
                var event:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_UP);
                event.keyCode = 9;
                //event.keyCode = Keyboard.TAB;
                dispatchEvent(event);
                delete this.chkBtn;
            }
        }

        return;
    }// end function
    public function onDown(e:KeyboardEvent):void 
    {
        trace(e.keyCode);            
    }
    public function onUp(e:KeyboardEvent):void 
    {
        trace(e.keyCode   " UP");
    }
    public function onBtn1(event:MouseEvent) : void     
    {
        trace("pessed btn1");
    }// end function
    public function onBtn2(event:MouseEvent) : void     
    {
        trace("pessed btn2")
    }// end function
    public function onBtn3(event:MouseEvent) : void     
    {
        trace("pessed btn3")
    }// end function    
    function frame1()
    {
        this.Btn1.addEventListener(MouseEvent.CLICK, this.onBtn1);
        this.Btn2.addEventListener(MouseEvent.CLICK, this.onBtn2);
        this.Btn3.addEventListener(MouseEvent.CLICK, this.onBtn3);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
        return;
    }// end function

}

  • Related