# Key detection

**URL:** <https://forum.kirupa.com/t/key-detection/266065>\
**Category:** flash\
**Created:** [July 15, 2008, 10:34pm UTC](https://forum.kirupa.com/t/key-detection/266065 "2008-07-15T22:34:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ismael\_92](https://avatars.discourse-cdn.com/v4/letter/i/c57346/32.png) [@Ismael\_92](https://forum.kirupa.com/u/Ismael_92)\
**Post date:** [July 15, 2008, 10:34pm UTC](https://forum.kirupa.com/t/key-detection/266065/1 "2008-07-15T22:34:51Z")

</div>

I’m new to AS3, and I’m going to make a simple game and I need to learn some things.

The most important thing I need to know is Key Detection.

I’ve read [this post](http://www.kirupa.com/forum/showpost.php?p=2098269&postcount=319) but I didn’t understand it at all. I also think there must be a simpler way to do that.  
So, I would be pleased if someone could post a code to make something happen when a key is pressed, and explain it a bit.

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 16, 2008, 1:26am UTC](https://forum.kirupa.com/t/key-detection/266065/2 "2008-07-16T01:26:04Z")

</div>

Here’s an example document class. I made it very basic, hope you get get something from it. Attached the FLA+AS as well.

```auto

package {
	
	import flash.display.Sprite;
	
	public class baseClass extends Sprite {
		
		import flash.events.KeyboardEvent;
		
		public function baseClass():void {
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, processKey); // Attach listener for Key_down event
		};
		
		private function processKey(e:KeyboardEvent):void { // Function is called when event fires
			trace("Key Code is: "+e.keyCode); // Keyboard code
			trace("Character Code is: "+e.charCode); // Character code
		};
	};
};

```

Good luck :beer2:
