brenjt
brenjt

Reputation: 16297

Trigger SimpleButton state with KeyDown Listener in AS3

I am looking for a way to trigger the downState of my SimpleButton using my KEY_DOWN event. I am hoping there is a way to do this without having to switch this to a MovieClip custom button.

Here is my code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

private function keyDown(e:KeyboardEvent):void
{
    switch(e.keyCode)
    {
        case 32:
            trace("Space Down");
            this.actionButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
    break;
    }
}

The actionButton is my SimpleButton. I have tried dispatcing the CLICK event and MOUSE_DOWN event but neither has worked. Any advice would be stellar.

Upvotes: 0

Views: 686

Answers (1)

eLouai
eLouai

Reputation: 696

Your code looks fine.

Sometimes the event handling within the Flash IDE program does not work properly. Particularly when dealing with keyboard events. The debugger somehow 'steals' those events. Test it outside the ide or debugger.

Let us know if that was the issue.

Upvotes: 0

Related Questions