Skip to main content
Known Participant
February 17, 2011
Answered

KeyboardEvent is not dispatching event

  • February 17, 2011
  • 2 replies
  • 1219 views

Hi

I´m making a game.

This game has 4 frames.

It has a preloader.swf and game.swf.

The preloader.swf loads the game.swf.

The game.swf has 4 frames.

In the first frame is given to the user

the options to play the game (clicking a button "playGame"

on "click" event) and to set the sounds settings (clicking a button "options"

on "click" event).

So far so good.

The problem is:

When the user clicks "playGame" I send the user to frame 2,

where it has the option to select a game level using the arrow keys.

So within the playGameListener I registered two keyboard events one

for KEY_UP and another for KEY_DOWN.

So, to have sure that it´s working, I added a trace for each keyboard event

listener. But I´m not getting any message in the output window while pressing

left arrow or right arrow or any arrow.

So I learned that KeyboardEvent only dispatches an event if the flash player has focus.

Well I think the flash player has never losed focus in this process.

I´ve registered two events for Event.ACTIVATE and Event.DEACTIVATE and inside its listeners

I added a trace with a message.

Flash outputs me a message if I minimize the flash player window or if I maximize the flash player,

but not while the game.swf´s frame transition.

The listeners for the KeyboardEvent works only when I press at the flash player, then I receive the

messages from the KeyboardEvent listeners.

There´s any workaround for it?

I´m thinking to create an array with 4 places.

The first place would be for the "IntroScreen" movieclip, the second for the "LevelChooseScreen", and so on.

What do you think?

Thanks

This topic has been closed for replies.
Correct answer Łukasz_Grela

Hi,

I have tested following:

stage.addEventListener(FocusEvent.FOCUS_IN, onFocusIn, false, 0, true);
stage.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut, false, 0, true);
stage.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, onKeyFocusChange, false, 0, true);
stage.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, onMouseFocusChange, false, 0, true);

stage.focus = this;

//...

protected function onMouseFocusChange(e:FocusEvent):void
{
     trace(e);
}

protected function onKeyFocusChange(e:FocusEvent):void
{
     trace(e);
}

protected function onFocusOut(e:FocusEvent):void
{
     trace(e);
}
         
protected function onFocusIn(e:FocusEvent):void
{
     trace(e);
}

and got following output:


[FocusEvent type="focusIn" bubbles=true cancelable=false eventPhase=3 relatedObject=null shiftKey=false keyCode=0]

then clicked on any object


[FocusEvent type="focusOut" bubbles=true cancelable=false eventPhase=3 relatedObject=null shiftKey=false keyCode=0]
[FocusEvent type="mouseFocusChange" bubbles=true cancelable=true eventPhase=2 relatedObject=[object Sprite] shiftKey=false keyCode=0]

so focusOut event is triggered but ONLY when stage was in focus first,

best regards

2 replies

February 17, 2011

Where did you added your events?

try like this:

import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN,fDown);

function fDown(evt:KeyboardEvent)
{
trace(evt.charCode);
}

Inspiring
February 17, 2011

Hi,

search for

stage.focus

in help, you said that after clicking in movie you have your keyboard events so by passing reference to the object you want to focus you will have youre keyboard listeners to work.

so after you have your transition finished you may call something like

stage.focus = this;

best regards

Known Participant
February 17, 2011

Thanks

Actually I´m using stage.focus = stage; now.

It works.

But I´m wondering if stage has not an event when it loses focus, as for example:

StageEvent.ON_LOSE_FOCUS thing.

Thank you