Flash KeyboardListner problem

A place for tutorials on how to get the most out of Flash

Flash KeyboardListner problem

Postby Haldor » Mon Feb 21, 2011 8:09 am

Can anyone who knows AS3 see anything wrong with this code? It compiles and runs fine, but the listener simply dosn't call the function no matter what key I press.
Mouse Listener and Frame Entry listeners both work correctly, just not keyboard ones =/

Code: Select All Code
package Script
{
   import flash.display.MovieClip;
   import flash.events.*;
   import flash.ui.Keyboard;
   
   public class Main extends MovieClip
   {
      public function Main()
      {
         addEventListener(KeyboardEvent.KEY_DOWN,keyD);
         addEventListener(KeyboardEvent.KEY_UP,keyU);
      }
      
      public function keyD(e:KeyboardEvent):void
      {
         trace("Debug");
      }
      
      public function keyU(e:KeyboardEvent):void
      {
         trace("Debug2");
      }
      
   }
}
"We could just... run away?"
Image
User avatar
Haldor
 
Joined: Thu Mar 25, 2010 10:29 pm
Location: Reach

Re: Flash KeyboardListner problem

Postby Renara » Mon Feb 21, 2011 12:02 pm

KeyboardEvents only work if the object you're coding has focus. If you want to add a global keyboard handler, then you should use stage.addEventListener() as it will pick up the event so long as any part of the Flash window has focus.
Renara (team twitter | newgrounds)
Team Lead Programmer
Lok Team Member
Site Administrator
User avatar
Renara
Foxy Admin
 
Joined: Fri Jan 08, 2010 4:16 pm


Return to Tutorials



Who is online

Users browsing this forum: No registered users