How to set input mode in C++

Due to the lack of documentation on the SetInputMode() in C++ I have resorted to asking this in the forums. How do you set the input mode in C++?

I opened up the source code in Visual Studio, and searched for SetInputMode().
The function is a member on a PlayerController.
It takes a FInputModeDataBase& const.
That class, in turn, is subclassed by three classes:
FInputModeUIOnly
FInputModeGameAndUI
FInputModeGameOnly

Thus, you need to create an instance of one of those three classes, and set up the instance the way you want it (widget to focus, etc.)
Make sure this instance lives for as long as you want the mode to be active! (Could be globals, or could be members of your player or whatever)
Then just call SetInputMode() on the appropriate player controller, passing the instance in question as argument.

There are command-line and blueprint callers of this function that you can read for even more examples.

3 Likes

Also does using UI only block game pad input? And as far as instancing the class its class FInputUIOnly input only? Much like declaring a variable?



	UWidgetBlueprintLibrary::SetInputMode_GameAndUI();
	UWidgetBlueprintLibrary::SetInputMode_GameOnly();
	UWidgetBlueprintLibrary::SetInputMode_UIOnly();


That’s how I do it. Each takes different params to build the correct struct to send to the player controller, I just find it easier to do it here.

Additionally, this changed in 4.13 but I don’t have that open atm so you’ll have to find the changes. Compiler should warn you anyway.

6 Likes

I think it fixed some of my issues for now I used it in blueprints however even now when i press “a” on my xbox the button doesnt click until i give it a legit mouse click and when i try to “click” another button my first one only clicks

This is how i did it :
PController->SetInputMode(FInputModeGameOnly()); or any of the other functions you want.

5 Likes

This is a blueprint library, and why someone want to use this if the game is C++ base? or there is some good sides of this library over C++ input mode?
controllerRef->SetInputMode(FInputModeGameOnly());

11 Likes