# Not sure about the best practice to use events/dispatch/listening in this case

**URL:** <https://forum.kirupa.com/t/not-sure-about-the-best-practice-to-use-events-dispatch-listening-in-this-case/323780>\
**Category:** Uncategorized\
**Created:** [July 18, 2011, 4:26am UTC](https://forum.kirupa.com/t/not-sure-about-the-best-practice-to-use-events-dispatch-listening-in-this-case/323780 "2011-07-18T04:26:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bontempos](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bontempos](https://forum.kirupa.com/u/bontempos)\
**Post date:** [July 18, 2011, 4:26am UTC](https://forum.kirupa.com/t/not-sure-about-the-best-practice-to-use-events-dispatch-listening-in-this-case/323780/1 "2011-07-18T04:26:39Z")

</div>

I’m reading a lot of examples and I know this is very easy, but I’m stuck =/

I’m using a class caled (KeyboardInputs) to dispatch events (keyboard/custom events…).

The idea is that this Class would dispatch all Key Down events so EVERY class could listen to it.  
Thing is, events are being dispatched ok, but I’m not sure the Classes are listening to it correctly. I mean. When I create a new instance of KeyboardInputs inside each class, and add a listener to it, are all the listener listening to the same events?

this is a sample of what I’ve got:

MainClass.as:

```auto

...extends Sprite...

```

KeyboardInput.as

```auto

package lib{
 
 import flash.events.*
 import lib.KeyMap;
 
 public class KeyboardInput extends MainClass
 {
   public function keyboardInput()
   {
    addEventListener(Event.ADDED_TO_STAGE,function(ev:Event):void
    {			
      stage.addEventListener(KeyboardEvent.KEY_DOWN,function(e:KeyboardEvent):void
      {
        keymap = new KeyMap();
	keymap.setKey(String.fromCharCode(e.charCode))
       });
      });			
   }
 }
}

```

KeyMap.as

```auto

package lib
{
	import flash.events.EventDispatcher;
	import lib.ConnectionEvent;
	
	
	public class KeyMap extends EventDispatcher
	{

		public function KeyMap()
		{}
		
		public function setKey(_k:String)
		{
			dispatchEvent(new ConnectionEvent(ConnectionEvent.ON_KEYPRESS,_k));
		}
	}
}

```

ConnectionEvent.as

```auto

package lib
{
	import flash.events.Event;

	public class ConnectionEvent extends Event
	{
		public static const ON_KEYPRESS:String = "onKeyPress";
		public var data:String;
		
		public function ConnectionEvent(type:String, ret:String = null)
		{
			super(type);
			if (ret != null) {
				data = ret;
			}
		}
		public override function clone():Event
		{
			return new ConnectionEvent(type);
		}
	}
}

```

HomeScreen.as

```auto

package scr{
 import flash.display.*
 import lib.*
 public class HomeScreen extends Sprite {
 
  public var _keymap:KeyMap;
  public function HomeScreen()
  {
   _keymap.addEventListner(ConnectionEvent.ON_KEYPRESS,keyAction);
   function keyAction(ev:ConnectionEvent)
   {
 **[COLOR="red"] trace("trace something")// <-- not outputing
    //DO SOMETHING HERE, BUT THIS IS NOT LISTENING TO NOTHING! =/[/COLOR]**
   }
  }
 }
}

```
