# Gamepad Connection Events

**URL:** <https://forums.unrealengine.com/t/gamepad-connection-events/334571>\
**Category:** Feedback & Requests\
**Tags:** gamepad, windows, unreal-engine, FeatureRequest, question\
**Created:** [October 7, 2015, 7:38pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571 "2015-10-07T19:38:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyperdr1ve](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hyperdr1ve](https://forums.unrealengine.com/u/hyperdr1ve)\
**Post date:** [October 7, 2015, 7:38pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571/1 "2015-10-07T19:38:55Z")

</div>

What is the recommended way to detect gamepad events? I’m thinking of the following scenarios:

- Gamepad is connected and gets disconnected
- Gamepad is disconnected and gets connected

Also, battery level would be nice. I’m interested in Windows only.

---

<div class="post-metadata">

**Author:** ![hyperdr1ve](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hyperdr1ve](https://forums.unrealengine.com/u/hyperdr1ve)\
**Post date:** [October 14, 2015, 6:49pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571/2 "2015-10-14T18:49:59Z")

</div>

I’ve looked through the source and can say that events for gamepads connecting/disconnecting do not exist. XInput has an XInputGetBatteryInformation call that isn’t plumbed. There’s also no way to query the number of detected gamepads. There’s quite a lot missing here.

---

<div class="post-metadata">

**Author:** ![HazzaBui](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HazzaBui](https://forums.unrealengine.com/u/HazzaBui)\
**Post date:** [October 23, 2015, 4:11pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571/3 "2015-10-23T16:11:57Z")

</div>

I did find a way to poll whether an Xbox controller is connected, although it may make your eyes bleed with terrible programming practice… (this needs to be implemented in C++)

At the top of your CPP file, add:

```
#if PLATFORM_WINDOWS
	#define private public
	#include "Runtime/Core/Public/Windows/WindowsApplication.h"
	#include "Runtime/Core/Private/Windows/XInputInterface.h"
	#undef private
#endif

```

Then implement a function to return whether an Xbox gamepads are connected:

```
bool XboxControllerConnected()
{
    #if PLATFORM_WINDOWS
    	if (FSlateApplication::IsInitialized() && FSlateApplication::Get().GetPlatformApplication().IsValid())
    	{
    		TSharedPtr<FWindowsApplication> app = StaticCastSharedPtr<FWindowsApplication>(FSlateApplication::Get().GetPlatformApplication());
    		if (app.IsValid())
    		{
    			TSharedPtr<XInputInterface> x = app->XInput;
    			if (x.IsValid())
    			{
    				for (int i = 0; i < MAX_NUM_XINPUT_CONTROLLERS; i++)
    				{
    					if (x->ControllerStates[i].bIsConnected)
    						return true;
    				}
    			}
    		}
    	}
    #endif
    	return false;
}

```

Huge disclaimer here though:  
This is not a good/sustainable/safe approach to solving this problem. Use at your own risk, and know that future updates of the engine could easily break this.

---

<div class="post-metadata">

**Author:** ![hyperdr1ve](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hyperdr1ve](https://forums.unrealengine.com/u/hyperdr1ve)\
**Post date:** [October 23, 2015, 4:18pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571/4 "2015-10-23T16:18:19Z")

</div>

Lol, yes my eyes are bleeding, but at least it’s something that works for now.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_478829031](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_478829031](https://forums.unrealengine.com/u/anonymous_user_478829031)\
**Post date:** [February 8, 2016, 8:47pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571/5 "2016-02-08T20:47:53Z")

</div>

This solution worked for me, but I opted for the engine change at the link below because it relies less on the dark arts.

> **[Is there a way to detect a gamepad?](https://forums.unrealengine.com/questions/142358/question-is-there-a-way-to-detect-a-gamepad.html)**
>
> Is there a way to detect if a controller is connected or is there an event called when a gamepadis connected? Also is there a generic event for getting input from a gamepad(if any button is hit for example)? It would be used for switching menu...

---

<div class="post-metadata">

**Author:** ![HazzaBui](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HazzaBui](https://forums.unrealengine.com/u/HazzaBui)\
**Post date:** [February 9, 2016, 4:18pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571/6 "2016-02-09T16:18:49Z")

</div>

Thanks for the update - that’s definitely a better solution if you are willing to compile your own engine

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_06f28069](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_06f28069](https://forums.unrealengine.com/u/anonymous_user_06f28069)\
**Post date:** [September 13, 2017, 1:46pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571/7 "2017-09-13T13:46:45Z")

</div>

Here is possible solution, only tested on windows so far.

File - New C++ Class  
Choose parent class - Blueprint Function Library, hit next  
Give it a name (for example GamepadDetection)

**GamepadDetection.h**

```
#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "GamepadDetection.generated.h"

/**
 * 
 */
UCLASS()
class PROJECTNAME_API UGamepadDetection : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:
	/* Check if gamepad is connected at Runtime. */
	UFUNCTION(BlueprintCallable, meta = (DisplayName = "GamepadConnected"), Category = "System Information")
		static bool IsGamePadConnected();
	
	
};

```

**GamepadDetection.cpp**

```
#include "GamepadDetection.h"
#include "SlateBasics.h"

bool UGamepadDetection::IsGamePadConnected()
{
	auto genericApplication = FSlateApplication::Get().GetPlatformApplication();
	if (genericApplication.Get() != nullptr && genericApplication->IsGamepadAttached())
	{
		return true;
	}
	return false;
}

```

Then finally uncommment/enable slate module depenecies within your projectname.Build.cs and reboot you editor before hitting compile.

YourProjectName.Build.cs

```
// Uncomment if you are using Slate UI
PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

```

Now you have a node inside blueprint to check if the gamepad is connected,

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_1cdfa42d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_1cdfa42d](https://forums.unrealengine.com/u/anonymous_user_1cdfa42d)\
**Post date:** [November 6, 2018, 7:03pm UTC](https://forums.unrealengine.com/t/gamepad-connection-events/334571/8 "2018-11-06T19:03:37Z")

</div>

If you connect to the `FCoreDelegates::OnControllerConnectionChange` delegate then you can be notified whenever a gamepad is connected/disconnected. Just make a function with the following signature:

`void OnControllerConnectionChanged(bool bIsConnected, FPlatformUserId UserID, int32 UserIndex);`
