Pausing a Windows game on application losing focus

If my Windows game loses focus mid-game for whatever reason (alt+tab, minimize, another application stealing focus etc.), I’d like to bring up the in-game pause menu so the player can gracefully resume the game by unpausing afterwards. But I’m having trouble finding where I can hook into this behaviour on the project-code side.

From what I can see, WM_ACTIVATE is handled in WindowsApplication.cpp, which sends OnWindowActivationChanged() to the current message handler, ie. FSlateApplication. But there doesn’t seem to be any easy way to pass this event through to the game side without making engine changes.

Is this the case, or am I missing something stupid?

see https://answers.unrealengine.com/questions/225658/application-lost-focus-event.html

The link above by getnamo doesn’t appear to produce the desired behaviour. On UE4.10 with a Win64 build, if I alt+tab in and out of my application, neither UGameViewportClient::LostFocus/ReceivedFocus is called.

Really? No response to this? I center the mouse cursor when in first person mode, so when the app loses focus, the mouse cursor is stuck at the center of the game window. Therefore a “lost focus” event is critical. Anyone or Rama know how to do this? I work primarily in Blueprint.

any luck on blueprint checking for window is focused?

Can’t help with blueprints but we have this C++ function in our base HUD class that inherits directly from the Unreal HUD class.



/**
* Return true if game window is focused.
*/
bool ABaseHUD::GetIsGameWindowFocused()
{
    if (GetWorld())
    {
        UGameViewportClient* vpClient = GetWorld()->GetGameViewport();
        if (vpClient)
        {
            FViewport* vp = vpClient->Viewport;
            if (vp)
            {
                return vp->IsForegroundWindow();
            }
        }
    }

    return false;
}


this could help: https://answers.unrealengine.com/questions/225658/application-lost-focus-event.html