# Execute Blueprint Functions when Application gets focus/loses focus

**URL:** <https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006>\
**Category:** XR Development\
**Tags:** focus, unreal-engine, hmd, question, Blueprint, CPP\
**Created:** [August 23, 2016, 1:09pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006 "2016-08-23T13:09:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil070](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phil070](https://forums.unrealengine.com/u/phil070)\
**Post date:** [August 23, 2016, 1:09pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/1 "2016-08-23T13:09:09Z")

</div>

Hey, the problem is as follows:

My VR Application starts an external VR application which is then in focus. Ending the Application brings the Unreal Application again in focus. The problem is, tho the application is running fine, the image freezes. This problem can be solved by disabling the HMD and enabling it again, for example when the key “R” is pressed. How can I automatically disable/enable the HMD when the application gets the focus back?

Regards

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_48f2118e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_48f2118e](https://forums.unrealengine.com/u/anonymous_user_48f2118e)\
**Post date:** [August 23, 2016, 6:15pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/2 "2016-08-23T18:15:17Z")

</div>

I think you’ll have to detect it in C++ and call out to your blueprint function.

> **[How to tell when application has lost focus (i.e alt tab)](https://forums.unrealengine.com/questions/301649/how-to-tell-when-application-has-lost-focus-ie-alt.html)**
>
> I want to do things when a player alt tab’s the game or something else causes him to lose focus from the whole game and onto another application. I heard about using UGameViewportClient::LostFocus, but it doesn’t work correctly. When someone clicks...

---

<div class="post-metadata">

**Author:** ![phil070](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phil070](https://forums.unrealengine.com/u/phil070)\
**Post date:** [August 24, 2016, 10:34am UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/3 "2016-08-24T10:34:40Z")

</div>

I already saw that thread but it`s not exactly what i want to do. I need an event that detects only once if the focus is lost or the focus got back. It goes more in the direction of UGameViewportClient::LostFocus and UGameViewportClient::ReceivedFocus but I`m not sure how to implement it. I`m relatively new to c++ and unreal programming. I already looked at [https://answers.unrealengine.com/questions/225658/application-lost-focus-event.html](https://answers.unrealengine.com/questions/225658/application-lost-focus-event.html)

---

<div class="post-metadata">

**Author:** ![XuruDragon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XuruDragon](https://forums.unrealengine.com/u/XuruDragon)\
**Post date:** [August 26, 2016, 1:31pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/4 "2016-08-26T13:31:03Z")

</div>

Hi phil070,

As you mention it, you should use UGameViewportClient::Lost/ReceiveFocus.

You have to create a class that derive from UGameViewportClient

Put this in the .h file :

```
#pragma once
 #include "Engine/GameViewportClient.h"
 #include "MyGameViewportClient.generated.h"
 
 /**
  * UMyGameViewportClient class.
  */
 UCLASS(Blueprintable)
 class GAME_API UMyGameViewportClient : public UGameViewportClient
 {
     GENERATED_BODY()
 
 public:
     // Override lost focus function | This function will call your blueprint function.
     virtual void LostFocus(FViewport* Viewport) override;

     // Override lost focus function | This function will call your blueprint function.
     virtual void ReceiveFocus(FViewport* Viewport) override;

     UFUNCTION(BlueprintImplementableEvent, Category="LostFocus")
     public void ActionToDoBeforeLostFocus();

     UFUNCTION(BlueprintImplementableEvent, Category="ReceiveFocus")
     public void ActionToDoAfterReceiveFocus();
 };

```

put this in de .cpp file:

```
#include "Game.h"
 #include "MyGameViewportClient.h"
 
 void UMyGameViewportClient::LostFocus(FViewport* Viewport)
 {
     UMyGameViewportClient::ActionToDoBeforeLostFocus(); //call our blueprint function before that the game engine execute his own LostFocus code.

     UGameViewportClient::LostFocus(Viewport); //call the parent method
 }
 void UMyGameViewportClient::ReceiveFocus(FViewport* Viewport)
 {         
     UGameViewportClient::ReceiveFocus(Viewport); //call the parent method

     UMyGameViewportClient::ActionToDoAfterReceiveFocus(); //call our blueprint function after that the game engine execute his own ReceiveFocus code.
 }

```

Now you juste to define a blueprint derive this class (UMyGameViewportClient) and override the ActionToDoBeforeLostFocus and ActionToDoAfterReceiveFocus in blueprint, thats it! enjoy!  
This function has no parameters, to acces any variable of the game use GameMode or GameInstance object

Let me know if it resolve your problem 😉

Best regards,  
XuruDragon

---

<div class="post-metadata">

**Author:** ![phil070](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phil070](https://forums.unrealengine.com/u/phil070)\
**Post date:** [August 29, 2016, 1:11pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/5 "2016-08-29T13:11:02Z")

</div>

Hi XuruDragon ,

thanks for your answer. I implemented the code as you said. Then created a new Blueprint derived from MyGameViewportClient. There I have the event Action To Do Before Lost Focus but the event is still not working.

![104765-lostfocus.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/e/0/ee0a10f649fcb1aae715a7b526027e6cbbbca8fa.png)

---

<div class="post-metadata">

**Author:** ![XuruDragon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XuruDragon](https://forums.unrealengine.com/u/XuruDragon)\
**Post date:** [August 29, 2016, 1:22pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/6 "2016-08-29T13:22:19Z")

</div>

Hi phil070,  
Maybe you should implement the event Action to do Before Lost Focus as a function by override it. (You should have an option in the blueprint editor)  
I think the events are governed by a system of dispatchers and I think the event is not just called at all. Moreover, in the C ++ code we have write the function to override as function. Yes I know this is confusing . I would try this evening after returning from work , to do a test on my platform if you stil blocked

---

<div class="post-metadata">

**Author:** ![phil070](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phil070](https://forums.unrealengine.com/u/phil070)\
**Post date:** [August 29, 2016, 1:32pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/7 "2016-08-29T13:32:32Z")

</div>

Yes, I have two functions available to override (See Screenshot). If you could test it that would be awesome 🙂 PS: you got a small type error in your code. It`s ReceivedFocus not ReceiveFocus. Thanks for your help!

![104766-override.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/8/c/68ca8758b0a214dea7c24f3920b69d95743697f2.png)

---

<div class="post-metadata">

**Author:** ![XuruDragon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XuruDragon](https://forums.unrealengine.com/u/XuruDragon)\
**Post date:** [August 29, 2016, 1:41pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/8 "2016-08-29T13:41:09Z")

</div>

Yep, I will test that tonigth around 10pm hour of paris beacause i’m a french guy. I will notify you after that.  
Thank you, it’s write from my memory , obviously I should have checked … ^^

Yes that’s right , you can override the function that we see on the screenshot , it should do the trick 😉

---

<div class="post-metadata">

**Author:** ![XuruDragon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XuruDragon](https://forums.unrealengine.com/u/XuruDragon)\
**Post date:** [August 29, 2016, 6:47pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/9 "2016-08-29T18:47:12Z")

</div>

i’m sorry but I’m blocked with my Visual Studio/Unreal Engine each time I want to open Visual STudio it crash…

By the way, my solution is not suitable, because we only can override event and they can not be fired up…

Sorry phil0770 i can’t help anymore… ☹  
You have to do it all by c++ way… 😕

---

<div class="post-metadata">

**Author:** ![Loden-Heathen](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/loden-heathen/32/753374_2.png) [@Loden-Heathen](https://forums.unrealengine.com/u/Loden-Heathen)\
**Post date:** [May 28, 2024, 2:51pm UTC](https://forums.unrealengine.com/t/execute-blueprint-functions-when-application-gets-focus-loses-focus/368006/10 "2024-05-28T14:51:50Z")

</div>

This is an old thread but still comes up when googling this topic

I think I have found the answer RE Blueprint

You can add the “Application Life Cycle” component to your object and that has delegates for Will Enter Background and Has Entered Foreground … which I think is what we are looking for in the case of

How to have a logic defined in a blueprint react to the app/game losing/gaining focus

Somewhat crazy this isn’t more clear you would think EVERY game needs to handle this at least on PC
