AS3 Key Manager

Keyboard

Keyboard

I have written a small class that enables programmers to access which keys are pressed at any given time. Its particularly useful for game development when you want to move a sprite every frame for example.

The class is pretty simple and also quite small. The class contains an 2 dimensional array, and when the KEY_DOWN listener is fired the Key Code is stored into the array and a Boolean for that Key Code set to true. When the KEY_UP listener is fired, the Boolean relating to that Key Code is set to false.

To keep it simple, to use the KeyManager all you need to do is pass it an instance on the stage using an init method. As all the public methods are static, this can be done by:

KeyManager.init( stage );

To find out if a key has been pressed, you simply pass through a Key Code to the isKeyDown method. Fortunately, the Adobe libraries come with a list of Key Codes which can be found in the flash.ui.Keyboard class. We can use these with the KeyManager to easily access if a key has been pressed , like so :

KeyManager.isKeyDown( Keyboard.SPACE );

If the key has been pressed, the KeyManager will return true, else false. It is a pretty quick and easy way to work with the keyboard.
Here is the full code listing for the KeyManager:

package com.grindheadgames.input
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;

	public class KeyManager
	{
		//a reference to the stage
		private static var stage:Stage,	
		//an array of pressed keys
		lib:Array = [];
		
		public function KeyManager()
		{
			
		}
		
		/**
		 * initilise the KeyManager
		 * 
		 * @param s a reference to the Stage.
		 * 
		 */		
		
		public static function init(s:Stage):void
		{
			stage = s;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown, false,0,true);
			stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp,false,0,true);
		}
		
		/**
		 * destroy the KeyManager and free up memory for garbage collection. 
		 */		
		
		public static function destroy():void
		{
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
		}

		/**
		 * handler for the KEY_DOWN event 
		 * @param e KeyboardEvent
		 * 
		 */		
		
		private static function handleKeyDown(e:KeyboardEvent):void
		{
			lib[e.keyCode] = true;
		}
		
		/**
		 * handler for the KEY_UP event
		 * @param e KeyboardEvent
		 * 
		 */		
		
		private static function handleKeyUp(e:KeyboardEvent):void
		{
			lib[e.keyCode] = false;
		}
		
		/**
		 * Use this method to determine if a key is down.  
		 * 
		 * @param keyCode Use the flash.ui.Keyboard class to determine if a key is down. Example: KeyManager.isKeyDown( Keyboard.Space )
		 * @return Boolean if the key is currently down, will return true, else false. 
		 * 
		 */		
		
		public static function isKeyDown( keyCode:uint ):Boolean
		{
			return lib[keyCode] as Boolean;
		}
	}
}

To demonstrate using the KeyManager, I have made a small application which shows the keyboard manager being used to control a small car. I am currently writing a tutorial on the physics and techniques used to create the car, but in the meantime I have commented each line and uploaded a ZIP folder of the files. You can download it here:

Example KeyManager with TopDownCar.as Example.

The project is a Flash Builder project, but it should work with an FLA too if you set the Document class to TopDownCarExample.as and save the FLA in the src folder.

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditShare on StumbleUponShare on TumblrEmail this to someone

2 thoughts on “AS3 Key Manager

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>