1
\$\begingroup\$

I want to pause my game (written in Java Swing), when the player switches to another application.

But I did not find a way to detect this switch. window.isActive() only works for multiple windows within one Java application. But it does return true, when a different application has the focus.

The same is true for the KeyboardFocusManager, it returns the window of my application that had the focus before the focus was given to another application.

\$\endgroup\$
  • 1
    \$\begingroup\$ While you may be making a game, this issue is not solely related to game development. "General programming questions more likely belong on Stack Overflow instead of here. A good rule of thumb is to ask yourself... Would a professional game developer give me a better/different/more specific answer to this question than other programmers?" I think this question fails that test. \$\endgroup\$ – MichaelHouse Jul 16 '13 at 17:08
2
\$\begingroup\$

Use the focus subsystem, as documented here.

An example of detecting focus being gained:

//Make textField get the focus whenever frame is activated.
frame.addWindowFocusListener(new WindowAdapter() {
    public void windowGainedFocus(WindowEvent e) {
        textField.requestFocusInWindow();
    }
});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.