Skip to content

Instantly share code, notes, and snippets.

@OmarShehata
Created June 1, 2017 21:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OmarShehata/a9f86e091fe14c050fc200a1fa31fa10 to your computer and use it in GitHub Desktop.
Save OmarShehata/a9f86e091fe14c050fc200a1fa31fa10 to your computer and use it in GitHub Desktop.
Rebinding Keys at runtime in Unreal Engine 4.
void RebindAction(FName ActionName, FKey NewKey) {
// Adapted from Rama's VictoryBPFunctionLibrary (https://wiki.unrealengine.com/Rebinding_Keys_At_Runtime_in_Packaged_Game)
UInputSettings* Settings = const_cast<UInputSettings*>(GetDefault<UInputSettings>());
if (Settings) {
TArray<FInputActionKeyMapping>& Actions = Settings->ActionMappings;
for (FInputActionKeyMapping& Each : Actions)
{
if (Each.ActionName == ActionName) {
Each.Key = NewKey;
UE_LOG(LogTemp, Warning, TEXT("%s is triggered by %s"), *Each.ActionName.ToString(), *Each.Key.ToString());
}
}
//SAVES TO DISK
const_cast<UInputSettings*>(Settings)->SaveKeyMappings();
//REBUILDS INPUT, creates modified config in Saved/Config/Windows/Input.ini
for (TObjectIterator<UPlayerInput> It; It; ++It)
{
It->ForceRebuildingKeyMaps(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment