KeyboardEvent not working?

So I got a Main Class and Import inside it an Elliptical animation Class each extends Sprite. Everything works with eventListener for MouseEvent. But when I changed it to keyboard event, it stops working!

My Main looks like this

class Main extends Sprite {
     var mcElliptical = new Elliptical()
     public function new(){
          addChild(mcElliptical);
     }
}

The working Elliptical class looks like this

class Elliptical extends Sprite {
    //private vars lots of it
    public function new(){
       initializeObjects();
    }
    private function initializeObjects(){
       // Assets bitmap and so on
       addEventListener(MouseEvent.MOUSE_DOWN, initializeTween);
    }
    private function initializeTween(e:MouseEvent){
        //many conditions here to lunch tween_0, tween_1, tween_2 and so on.
    }
    private function tween_01(){
        //Several Actuate.tween here
    }
   .
   .
   .
}

When I replaced the listener to Keyboard event,

addEventListener(KeyboardEvent.KEY_DOWN, initializeTween);
.
.
.
private function initializeTween(e:KeyboardEvent) ...

it compiles ok, no error but animations was not triggered when I press any keys on my keyboard, no trace output either. When I return it back to mouse event, things animate when I click my mouse, so I was sure that my problem was activating the keyboard event. I dont know what I did wrong, I coded it like any other event does. Any help?

By the way, tried the following with no luck:

stage.addEventListener(KeyboardEvent.KEY_DOWN, initializeTween), 
this.addEventListener(KeyboardEvent.KEY_DOWN, initializeTween);
this.parent.addEventListener(KeyboardEvent.KEY_DOWN, initializeTween); 
Lib.current.addEventListener(KeyboardEvent.KEY_DOWN, initializeTween);

Sounds stupid, but if you’re compiling to html5, are you clicking on the canvas before pressing keys? I’ve found that it takes a couple clicks for the canvas to properly focus sometimes.

yhea, really, I don’t know what is happening or don’t know what am I doing, I am compiling for flash. clicked a lot of times, smashed my keyboard, no luck. When in mouse event, just click it gently and behold!

Try listening to the stage for keyboard events, I don’t think they bubble down like mouse events do :slight_smile:

i tried it, when I listen from the Elliptical class like this

stage.addEventListener(params here);

I get bunch of errors:

stage is undefined reference, or cannot access undefined reference. null object and so on.

Nevertheless, I have been successful to make a workaround by placing the listener directly on my Main.hx, then address the receiving function from the instance of Elliptical class.

In case other people bumps here, this is how I made it work:

class Main extends Sprite {
     var mcElliptical = new Elliptical()
     public function new(){
          addChild(mcElliptical);
          //place keyboard listener here, avoid placing it on other external class
          stage.addEventListener(Keyboard.KEY_DOWN, mcElliptical.initializeTween); 
     }
}

//avoid placing keyboard listener here.
class Elliptical extends Sprite{
   //vars here
   public function initializeTween(e:KeyboardEvent){
       if(e.UP){
           //do something
       }else if (e.DOWN){
          //do something else
       }
   }
}

Oh, stage is null before you add something to the stage, so you’ll need to wait until after it’s added (by convention or waiting for Event.ADDED_TO_STAGE) or you can design a system that goes top-down, handling events up top and propagating actions down to objects that need it :slight_smile: