Skip to main content
October 29, 2016
Answered

Using a symbol buttons as a keyboard buttons in a mobile AIR application

  • October 29, 2016
  • 2 replies
  • 678 views

I have four buttons in my stage and I would like to set every button with its equivalent in keyboard buttons, you can say like a shortcut buttons. this is the instance name of my buttons with keyboard buttons.

  • "btn1" for Left arrow
  • "btn2" for Right arrow
  • "btn3" for Up arrow
  • "btn4" for Down arrow

I added an eventListener to every button, but I don't know what can I put inside the function.

btn1.addEventListener(MouseEvent.CLICK, Left);
function Left(event:MouseEvent😞void
{
//Left arrow was pressed
}

is there any way to do this?

Thanks.

This topic has been closed for replies.
Correct answer Ned Murphy

You cannot control the keyboard with actionscript, but you can mimic what the keyboard keys do by programming the buttons to take the same actions as what you code for the keys.   If you code the right arrow keyboard key to make something move right, then you use that same moving right code for the button that you want to make things move right.

Example...

var goLeft:Boolean = false;  // create goRight, goUp, goDown as well for the other controls

function hearKeyDown(yourEvent:KeyboardEvent):void{    // you can include all the keys in here

    if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = true; }   

    stage.addEventListener(Event.ENTER_FRAME, moveObject);

}

function hearKeyUp(yourEvent:KeyboardEvent):void{        // you can include all the keys in here

    if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = false; }   

    stage.removeEventListener(Event.ENTER_FRAME, moveObject);

}

function hearMouseDown(yourEvent:MouseEvent):void{    // you can include all the buttons in here

    if (yourEvent.currentTarget==btn1){ goLeft = true; }   

    stage.addEventListener(Event.ENTER_FRAME, moveObject);

}

function hearMouseUp(yourEvent:MouseEvent):void{    // you can include all the buttons in here

    if (yourEvent.currentTarget==btn1){ goLeft = false; }   

    stage.removeEventListener(Event.ENTER_FRAME, moveObject);

}

function moveObject(evt:Event):void {                                // share this for all the keys and buttons

    if (goLeft){ object_mc.x-=5 };   

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKeyDown);

stage.addEventListener(KeyboardEvent.KEY_UP, hearKeyUp);

btn1.addEventListener(MouseEvent.MOUSE_DOWN, hearMouseDown);

btn1.addEventListener(MouseEvent.MOUSE_UP, hearMouseUp);

2 replies

Ned Murphy
Legend
October 30, 2016

Please reread my response - What you just asked for is what my posting explained.

Herman Jose
Participant
December 12, 2018

Hello Master Ned, I'm new to As3, my question is also this ... please try to implement this technique, but give me errors, and I do not know how to solve them ... I know it will sound a little rough, but you could Please pass this code to me in a more basic way with examples please

Ned Murphy
Ned MurphyCorrect answer
Legend
October 29, 2016

You cannot control the keyboard with actionscript, but you can mimic what the keyboard keys do by programming the buttons to take the same actions as what you code for the keys.   If you code the right arrow keyboard key to make something move right, then you use that same moving right code for the button that you want to make things move right.

Example...

var goLeft:Boolean = false;  // create goRight, goUp, goDown as well for the other controls

function hearKeyDown(yourEvent:KeyboardEvent):void{    // you can include all the keys in here

    if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = true; }   

    stage.addEventListener(Event.ENTER_FRAME, moveObject);

}

function hearKeyUp(yourEvent:KeyboardEvent):void{        // you can include all the keys in here

    if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = false; }   

    stage.removeEventListener(Event.ENTER_FRAME, moveObject);

}

function hearMouseDown(yourEvent:MouseEvent):void{    // you can include all the buttons in here

    if (yourEvent.currentTarget==btn1){ goLeft = true; }   

    stage.addEventListener(Event.ENTER_FRAME, moveObject);

}

function hearMouseUp(yourEvent:MouseEvent):void{    // you can include all the buttons in here

    if (yourEvent.currentTarget==btn1){ goLeft = false; }   

    stage.removeEventListener(Event.ENTER_FRAME, moveObject);

}

function moveObject(evt:Event):void {                                // share this for all the keys and buttons

    if (goLeft){ object_mc.x-=5 };   

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKeyDown);

stage.addEventListener(KeyboardEvent.KEY_UP, hearKeyUp);

btn1.addEventListener(MouseEvent.MOUSE_DOWN, hearMouseDown);

btn1.addEventListener(MouseEvent.MOUSE_UP, hearMouseUp);

October 30, 2016

Thank you sir for your reply, but I don't want to control the keyboard, I just want to make these buttons equal to keyboard arrow keys and doing the same things if they pressed.

I am using these buttons for mobile to control an external flash game.