1 Reply Latest reply on Aug 31, 2011 8:12 AM by saisri2k2

    Trying to catch keystrokes

    iamcootis Level 1

      I am trying to write an autonomous KeyCatcher class that I can instantiate anywhere in my application and use it. Unfortunately I am stuck on the very first step. My code looks exactly like the code below. The listener functions are never being called when I hit keys. What am I doing wrong?

       

      Thanks for your help.

       

       

      [CODE]

      var kc:KeyCatcher = new KeyCatcher();
      kc.start();

      [/CODE]

       

       

      [CODE]

      public class KeyCatcher extends EventDispatcher
          {
              public function KeyCatcher()
              {
              }
             
              public function start():void{
                  addEventListener(KeyboardEvent.KEY_DOWN, catchDownKey);
                  addEventListener(KeyboardEvent.KEY_UP, catchUpKey);
              }
             
              private function catchDownKey(evt:KeyboardEvent):void{
                  trace(evt);
              }
             
              private function catchUpKey(evt:KeyboardEvent):void{
                  trace(evt);
              }
          }

      [/CODE]

        • 1. Re: Trying to catch keystrokes
          saisri2k2 Level 4

          It depends on where you registered your events. I normally listen on systemManager.addEventLIstener for this purpose of listening to the global events, in the main <s:Application file. the example you have does not listen to events on any things, all you are doing is creating an object and adding listener on itself which no one has any idea of who it is.