Download

Rebinding input in C++

Hi,

I’m trying to give users a possibility to change the input at runtime. I have the following code:


  
 #include "GameFramework/InputSettings.h"
#include "Components/InputKeySelector.h"
#include "GameFramework/PlayerInput.h"
 


  
void UPauseMenuWidget::NativeConstruct() {
    Super::NativeConstruct();

    TArray <class FInputActionKeyMapping> OutMappings;
    UInputSettings* InputSettings = UInputSettings::GetInputSettings();
    InputSettings->GetActionMappingByName("GrabObject", OutMappings);
    FInputActionKeyMapping KeyMapped = OutMappings[0];
    UInputKeySelector* GrabObjectInputKeySelector = nullptr;

    GrabObjectInputKeySelector->SetSelectedKey(KeyMapped.Key);
    GrabObjectInputKeySelector->OnKeySelected.AddDynamic(this, &UPauseMenuWidget::OnChangeJump);

}


And the function bound to the OnKeySelected delegate:



void UPauseMenuWidget::OnChangeGrabObject(FInputChord InputChord) {
    TArray <class FInputActionKeyMapping> OutMappings;
    UInputSettings* InputSettings = UInputSettings::GetInputSettings();
    InputSettings->GetActionMappingByName("GrabObject", OutMappings);
    FInputActionKeyMapping KeyMapped = OutMappings[0];

    UInputSettings* InputSettings = UInputSettings::GetInputSettings();
    InputSettings->RemoveActionMapping(KeyMapped);
    //KeyMapped = InputChord.Key;
    InputSettings->AddActionMapping(KeyMapped);

}
 

I’m missing something and I don’t know what.

How to convert **InputChord **parameter(going from the OnKeySelected delegate) to FInputActionKeyMapping variable to use in the InputSettings->AddActionMapping function?

It should be done in this way:

KeyMapped = FInputActionKeyMapping(“GrabObject”,InputChord.Key);