views:

86

answers:

1

Hey,

I'm trying to get familiar with the whole keyboard event detection thing.

Here's my sample code.

<fx:Script>
    <![CDATA[

        import flash.events.KeyboardEvent;
        import mx.controls.Alert;

        private function init():void{
            addEventListener(KeyboardEvent.KEY_DOWN,reportKeyDown);   
        }

        private function reportKeyDown(event:KeyboardEvent):void {
            Alert.show("a key was pressed");
        }

    ]]>
</fx:Script>

As you can see, I'm at stage 0 of playing around with it, but it won't work. Anyone has any idea what I should be doing instead?

Thanks

+2  A: 

Try this:

stage.addEventListener(KeyboardEvent.KEY_DOWN,reportKeyDown);
Jorge
stage gives an error because it's still null at that point. I'd have to listen to the stage event first, then listen to this event. I wonder if there's a better way.
david
He's right. Just call your init at addedToStage, not creationComplete. At addedToStage, you're certain the stage property is set. HTH.
keyle