How to Detect Alt Tab / Lost Window Focus?

Currently if a user is playing in fullscreen mode and Alt+Tab’s out of the game, upon switching back to the game window, the game will no longer be fullscreen. This is somewhat inconvenient for the player, as they then need to open the options menu and toggle the fullscreen setting twice to return to fullscreen mode. Is there any cross-platform way to detect when the MonoGame application loses focus to compensate for this?

A Game object has an IsActive boolean flag. I know this changes depending on whether or not the window is currently selected and you can use that to detect when focus has been regained. I suspect you can do the same with an alt-tab and restore. Just check it in your update loop, saving the previous value so you can compare.


That’s exactly what I was looking for, thank you! I wrote this tiny method to handle switching back to fullscreen after tabbing out:

        /// <summary>
	/// check when the application loses and then re-gains focus, then double-toggle fullscreen if necessary
	/// </summary>
	void checkAppRegainedFocus() {
		if (!this.IsActive) {
			wasFocused = false;
		else if (!wasFocused && graphics.IsFullScreen) {
			wasFocused = true;
1 Like

What is the purpose of the two “graphics.ToggleFullScreen()” lines?

In my experience (on a Windows 7 64 bit development machine) Alt + Tabbing out of a fullscreen monogame app and then tabbing back in causes the app to switch to windowed mode. However, the graphics device still thinks it’s fullscreen. By toggling fullscreen mode twice I am able to negate this bug.

Can’t you just do graphics.aplychanges()

No, graphics.applyChanges() Does not have any effect in this situation. The application remains windowed while fully believing that it is still fullscreen.