Take the 2-minute tour ×

Why KeyboardEvent listener (AS3) does not react until I add it to the stage and not when I simply write it down in the Document Class just like I do with any other function? I mean, I have to write

stage.addEventListener(KeyboardEvent.KEY_DOWN, someFunc);

and not simply

addEventListener(KeyboardEvent.KEY_DOWN, someFunc);

just like I do with the others?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

you actually can... but before you need to define the stage.focus to the object you want to listen to the KeyboardEvent.KEY_DOWN event.

stage.focus = this
this.addEventListener(KeyboardEvent.KEY_DOWN, someFunc);

In the as3 reference guide you can see more info about the KeyboardEvent:

To listen globally for key events, listen on the Stage for the capture and target or bubble phase.

This is quite useful if you want to listen the KeyboardEvent on a TextField for example:

text_tf.addEventListener(KeyboardEvent.KEY_DOWN, someFunc);

Hope this clarifies your doubt.

share|improve this answer
add comment

Your Answer


