ActionScript3 KeyboardEvent

Hi everyone! I decided to move my old blog here. So slowly one by one I’ll post my archives here. Here you can read my first post „Ascii to Flash ActionScript Key Codes”. Today I will show you guys how to use ActionScript3 KeyboardEvent.

 

 

So how this work?

We simple add addEventListener to our stage, and after that we need to create four function for eventListener to fire event when key is pressed.

This simple script will do the magic:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keysDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keysUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keysLeft);
stage.addEventListener(KeyboardEvent.KEY_UP, keysRight);

function keysDown(event: KeyboardEvent) {
if(event.keyCode == 40) {
landinPad_mc.y +=5;

}

}

function keysUp(event: KeyboardEvent) {
if (event.keyCode == 38)
landinPad_mc.y -=5;

}

function keysLeft(event: KeyboardEvent) {
if(event.keyCode == 37) {
landinPad_mc.x -=5;

}

}

function keysRight(event: KeyboardEvent) {
if (event.keyCode == 39)
landinPad_mc.x +=5;

}

Pictures source ComputerHope.com