• entries
28
18
• views
5173

# Unreal Engine 4: Dynamically Changing Input Bindings

806 views

I have been working on a new game.  This one I'm going to be using mostly C++ in this one. I try to add something in each new game I work on that I haven't done before. This time its changing the input bindings at runtime. A lot of searching turn up a lot of old and not very useful information. So I figured I would provide a working way to do this without having to spend hours trying to find good and current information. So here it goes.

First the main class used for altering input bindings is UInputSettings. The key functions being RemoveAxisMapping, AddAxisMapping, RemoveActionMapping, AddActionMapping and SaveKeyMappings. We can create an instance of the class like this

UInputSettings *Settings = const_cast<UInputSettings*>(GetDefault<UInputSettings>());

Then its easy to add or remove axis and action mappings.

Settings->RemoveAxisMapping(FInputAxisKeyMapping(AxsName, FKey(*OldKey)));

Settings->RemoveActionMapping(FInputAxisKeyMapping(AxsName, FKey(*OldKey)));
Settings->AddActionMapping(FInputAxisKeyMapping(AxsName, FKey(*NewKey)));

And when you're done just save the changes

Settings->SaveKeyMappings();

Here is a function I am using to update an action mapping. You must remove the current mapping or both will exist. If the current key to jump is space and you change it to J, if you don't remove space the player can then hit either space or J to jump.

void USurvivalGameInstance::UpdateActionMapping(FName ActName, FString OldKey, FString NewKey)
{
UInputSettings *Settings = const_cast<UInputSettings*>(GetDefault<UInputSettings>());
if (!Settings) { return; }

Settings->RemoveActionMapping(FInputActionKeyMapping(ActName, FKey(*OldKey)));
Settings->SaveKeyMappings();
}

How do you get the currently pressed key?

UMG TextBox ?

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.