

When I started getting a hang of new stuff introduced in AS3, I got to notice that it has done away with a bunch of commands that were available in AS2. One of these missing commands included “Key.isDown” and added too that there were simply no key objects in AS3. Instead it has an alternative process whereby key codes can be accessed by name basically utilizing the keyboard class.
Now lets take a closer look; in AS3 in “Key.isDown” is basically an event handling process which is made out of a two step process
To elaborate the above concept of keyboard event handling Actionscript3 has build in “KeyboardEvent” and “keyboard” class. keypress event can be elaborated as
addEventListener(KeyboardEvent.KEY_UP,keyHandlerfunction)
Or
addEventListener(KeyboardEvent.KEY_down, keyHandlerfunction)
Here KEY_UP and KEY_DOWN are constants for respective key up or key down events. Key down event takes place when a key is pressed and reverse Key up event takes place when a pressed key is released.
keyHandlerfunction function will be executed in response to key up or key down event.
package
{
import flash.display.DisplayObject;
import flash.events.*;
public class Event_Example extends DisplayObject {
private var child: DisplayObject = new DisplayObject ();
// register the listener function
public function Event_Handle_Example()
{
addChild(child);
child.addEventListener(KeyboardEvent.KEY_UP, keyHandlerfunction);
}
// listener funtion
private function keyHandlerfunction (event:KeyboardEvent):void
{
trace(“keyHandlerfunction: “ + event.keyCode);
}
}
If you would like to make a comment, please fill out the form below.
| Cox services ||| Verizon Offers ||| Comcast cable offers |
nice tutorial..
Hey this is very good and simple example to understand Key handling in AS3. It is really helpfull.