OnApplicationFocus not called when returning to game with Win+D (Windows)

Hi,

Unity seems to have trouble handling focus statuses when using Win+D on Windows (it brings the user to his desktop when windows are open, and reopen the windows if the user is already on the desktop).
Specifically, OnApplicationFocus can be called several times on a row with “False” as argument value, and sometimes the game doesn’t regain focus despite being brought back on the foreground.

I have found two issues related to that:

  • In Windowed mode:
  1. With the game on foreground, press Win+D to go the the desktop: OnApplicationFocus(false) is correctly called.
  2. Press Win+D again: it brings the game back on the foreground, but OnApplicationFocus(true) is not called, and pressing keys on the keyboard makes a Windows error sound.
  3. Click somewhere else on your screen: OnApplicationFocus(false) is called again.
  4. Click on your game window: OnApplicationFocus(true) is finally called.
    Note: Depending on your setup, the game isn’t always brought back when pressing Win+D again, other programs can be brought in foreground instead. If it happens, try to reduce all other windows, or only have the game open.
  • In fullscreen mode:
  1. Press Win+D: OnApplicationFocus(false) is correctly called.
  2. Press Win+D again: the game is not brought back on the foreground, and OnApplicationFocus(false) is called again (can be repeated).
  3. Bring the game on the foreground by clicking on it in the bottom bar: OnApplicationFocus(true) is correctly called.

Not entirely sure what is on Unity’s side and what is on Windows’ side, but I think at least OnApplicationFocus should never be called several times in a row with the same value (unless I’m missing some “normal” way to trigger that), and at best there shouldn’t be any issue with that (the game correctly regain focus when windowed, and is correctly brought back in foreground when fullscreen).

I have reproduced the issues in an empty project for which I have joined to this topic two screenshots and the three assets to reproduce it.

Anyone has a workaround for this? Anyone from Unity knows if it is already on the roadmap?

PS: It looks like on other configs, the bug can be a lot worse. On my professionnal project, testers reported that the game was zoomed in, as if there were severe resolutions issues, once coming back on the game with Win+D. I haven’t been able to reproduce it on my computer.

2681557–189504–FocusWinDAssets.rar (6.5 KB)

No Unity people around here?

OnApplicationFocus is only called when the game view itself gets or loses focus, not when the entire editor gets or loses focus, which is a bug in my opinion.

1 Like

Did anyone figure this out? I also want this to be called when Unity editor loses focus, not the game view.

I don’t recall how this ended, but I’ll just add that my initial issues were related to game builds. The behavior in editor is more modular (and a lot less important anyway).