Pages

Thursday, October 7, 2010

Keyboard Events Won't Work Unless User Clicks Inside the Flash Movie First (and how to remove the yellow border around objects that are in focus)

If you find that, for some reason, you'll have to click inside your Flash movie first before the keyboard event listeners start working, then try adding a stage.focus statement to your code:
stage.focus = this;

Place this on the main timeline in order to set the focus to your Flash movie. Focus refers to the currently active object in your Flash movie. Whenever you click on something, like a MovieClip or a TextField, it becomes the active object and becomes the object that is in focus. Here, we are setting the focus back to the entire Flash movie without having to click on anywhere inside the movie. This way, the keyboard events that you've created will work right away. I've encountered this problem a couple of times and I was able to solve them by adding this line. However, adding this line might cause a yellow border to appear around the object that is in focus. If that happens, then try adding this line before the stage.focus statement:
stage.stageFocusRect = false;
stage.focus = this;

That yellow border is the stage focus rectangle which serves to highlight the object that is currently in focus. Here, we're setting the stageFocusRect property to false so that the border will not show up.

6 comments:

  1. Thanks for the tip. This is exactly what I was looking for!

    ReplyDelete
  2. Oh my word, I've been looking forever to solve this problem, I owe you <3

    ReplyDelete
  3. awesome! thank you so much!!

    ReplyDelete
  4. THANK YOU SO MUCH !!

    ReplyDelete
  5. In firefox version 17 first click won't work need to click again then only it accept keyboard event. pls help me.


    Thanks in advance

    ReplyDelete