Copy link to clipboard
Copied
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.
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.
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{
...Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Please reread my response - What you just asked for is what my posting explained.
Copy link to clipboard
Copied
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
Find more inspiration, events, and resources on the new Adobe Community
Explore Now