Application lost focus event

Hi,

I’m searching to set a pause menu and pause the game when lost the focus of the application but I don’t find a lost focus event.

Is it possible to know when the window lost focus ? How ?

Thanks

I think you are looking for:

UGameViewportClient::LostFocus

You can derive from this class and make your own functionality, of course it needs to be done in c++.

The engine must be changed and recompiled or a way exists to add an event without doing that ?

Nope, only your c++ game module.

I added c++ code like that :

#pragma once

#include "Engine/GameViewportClient.h"
#include "MyGameViewportClient.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FLostFocusSignature);

/**
 * UMyGameViewportClient class.
 */
UCLASS()
class GAME_API UMyGameViewportClient : public UGameViewportClient
{
  GENERATED_BODY()

public:

  // Lost focus event.
  UPROPERTY(BlueprintAssignable, Category="LostFocus")
  FLostFocusSignature OnLostFocus;

  // Override lost focus function.
  virtual void LostFocus(FViewport* Viewport) override;
};

#include "Game.h"
#include "MyGameViewportClient.h"

void UMyGameViewportClient::LostFocus(FViewport* Viewport)
{
  UGameViewportClient::LostFocus(Viewport);
  OnLostFocus.Broadcast();
}

The problem is I don’t see the event in my game mode blueprint to call a pause game and show a pause menu, and I don’t find a node in blueprint to get the game viewport (maybe not possible). Is it possible to have a global event spawned to have access in game mode blueprint ?

Push your event throught gameinstance and the event will be globally, you can obtain game instance from BP. And you can obtain your game instance from UMyGameViewportClient.

Ok, that works but sometimes the event is not sent, I don’t know why, actually not 100% safe to use.

#pragma once

#include "Engine/GameInstance.h"
#include "LostFocusGameInstance.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FLostFocusSignature);

/**
 * ULostFocusGameInstance class.
 */
UCLASS()
class GAME_API ULostFocusGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:

	// Lost focus event.
	UPROPERTY(BlueprintAssignable, Category="LostFocus")
	FLostFocusSignature OnLostFocus;
};

#pragma once

#include "Engine/GameViewportClient.h"
#include "MyGameViewportClient.generated.h"

/**
 * UMyGameViewportClient class.
 */
UCLASS()
class GAME_API UMyGameViewportClient : public UGameViewportClient
{
	GENERATED_BODY()

public:

	// Override lost focus function.
	virtual void LostFocus(FViewport* Viewport) override;
};

#include "Game.h"
#include "MyGameViewportClient.h"
#include "LostFocusGameInstance.h"

void UMyGameViewportClient::LostFocus(FViewport* Viewport)
{
	UGameViewportClient::LostFocus(Viewport);
	static_cast<ULostFocusGameInstance*>(GameInstance)->OnLostFocus.Broadcast();
}

Would be so nice to get an event like this - in blueprint…

1 Like

if someone having issues with UGameViewportClient::LostFocus
then another possible solution is:

// the following code is qouted from: https://benui.ca/unreal/window-focus-change/
void ABUIPlayerController::BeginPlay()
{
	FSlateApplication::Get().OnApplicationActivationStateChanged()
		.AddUObject( this, &ABUIPlayerController::OnWindowFocusChanged );
}

void ABUIPlayerController::OnWindowFocusChanged( bool bIsFocused )
{
// Don't pause in the editor, it's annoying
#if !WITH_EDITOR
	if ( bIsFocused )
	{
		// Unlimit game FPS
		GEngine->SetMaxFPS( 0 );

		// Unpause the game
		// MyHUD->SetPause( false );
	}
	else
	{
		// Reduce FPS to max 10 while in the background
		GEngine->SetMaxFPS( 10.0f );

		// Pause the game, using your "show pause menu" function
		// MyHUD->SetPause( true );
	}
#endif
}

thanks to Ben @ Pause Game When Window Loses Focus · ben🌱ui

1 Like