views:

325

answers:

2

I've written a little video game in Flex that runs in a browser. The player moves by pushing the arrow buttons on the keyboard, so I need to capture those keystrokes. In fact, the game action starts when the player presses one of those keys.

In order to capture the keystrokes, the Flash/Flex application, not just the browser, needs to have the focus.

How can I ensure that the application has the focus? I've implemented a bit of a hack: A "Begin" button you must click to start the game. The only point of this button is to ensure that the app has the focus. Is there a better solution to this?

+1  A: 

No, this is the only way, but I think your present solution is a great one. The reason that you (as a user) have to click to focus, is so that the application cannot quietly steal focus, to then log the keystrokes without your knowledge, e.g. to steal passwords.

richardolsson
+1  A: 

In some browsers (IE) you can give a SWF focus via JavaScript. Unfortunately this doesn't work in Firefox. So some users will have to click on the SWF to give it focus. You could pretty easily in your game check the browser and if it's IE then give the SWF focus automatically and not show the "Begin" button. Then in Firefox show the "Begin" button.

James Ward