Skip to content


[AS 3] Key Events with Actionscript

Here is a simple way of capturing keyboard events.

package
{
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;

	public class KeyEvents extends MovieClip
	{
		public function KeyEvents ()
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDownFX);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUpFX);
		}

		public function onKeyDownFX (event:KeyboardEvent)
		{
			trace ("Key is DOWN!");
			trace ("--------------")
			trace ("Key Code => " + event.keyCode);
			trace ("Control Key (Boolean) => " + event.ctrlKey);
			trace ("Shift Key (Boolean) => " + event.shiftKey);
			trace ("Alt Key (Boolean) => " + event.altKey);
		}

		public function onKeyUpFX (event:KeyboardEvent)
		{
			trace ("Key is UP!");
			trace ("--------------")
			trace ("")
		}
	}
}

Posted in Actionscript 3.


3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Zack says

    nice! thanks!

  2. Sathish says

    How does one remove these event listeners from stage?? Can you please explain

  3. anonimo says

    NICE !! thanks



Some HTML is OK

or, reply to this post via trackback.