You can call IsForegroundWindow() on your LocalPlayer’s ViewportClient->Viewport.
Something like:
ULocalPlayer* LocPlayer = Cast<ULocalPlayer>(Player);
if (!LocPlayer->ViewportClient->Viewport || !LocPlayer->ViewportClient->Viewport->IsForegroundWindow())
{
// viewport is either not present or not in the foreground.
}
…should work for you. Player lives within the PlayerController, so the above code will work if called from within a playerController, or can be called somewhere else by grabbing the Player from the local playerController.