Key Class In Flash – Part1

In this tutorial I will show you how to interact with the keyboard in Flash using the Key Class:

1- craete a new flash movie and select the first frame
2- open Actions window and type this code

var listener:Object = new Object;
Key.addListener(listener);
listener.onKeyDown = function():Void{
   trace(Key.getCode());
}

Now, click on Control&Enter to test the movie and press on keyboard keys, you should see some numbers in the output panel every time you click a keyboard button

Explanation:


var listener:Object = new Object;
This line just creates an object named “listener”.
————
Key.addListener(listener);
Here we are telling “listener” to start listening to the Key class.
—————-
listener.onKeyDown = function():Void{

}
Now we are declaring an event the belongs to the Key class which is onKeyDown and what it does that it runs the function that it is attached to every time a keyboard button is clicked.
———–
trace(Key.getCode());
Trace outputs text to the output panel while testing movie in runtime and here I told it to output the getCode() Property which is associated with the key class and all it does is returning the code of the clicked button.

Advertisements
This entry was posted in Software and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s