How to save / load keybindings

As title says, how do you save or load keybindings when a player changed them on runtime — for example, using PlayerPrefs or some ini file? So far, I haven’t found answer to this question in documentation or manuals.

P.S. I am speaking about new input system, of course.

code sample:

// save overridePath from PerformInteractiveRebinding() OnComplete
private void completeControl(InputActionRebindingExtensions.RebindingOperation rO)
        {
            if(rO.selectedControl != null)
            {
                aIdWaitingToGetCompleted.action.ApplyBindingOverride(rO.selectedControl.path);
                PlayerPrefs.SetString(aIdWaitingToGetCompleted.action.name, aIdWaitingToGetCompleted.action.bindings[0].overridePath);    // the overridePath
                PlayerPrefs.Save();
            }
        }

// apply override, use existing path if no override found
iA_rotate.ApplyBindingOverride(PlayerPrefs.GetString(iA_rotate.name, iA_rotate.bindings[0].path));

aIdWaitingToGetCompleted is a custom struct I use between input detection start and OnComplete handler. To get the action only, you can do so from rO too.

Also you might want to handle OnCancel.

The key for SetString is up to you of course, I use the name of the InputAction.

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_PerformInteractiveRebinding_UnityEngine_InputSystem_InputAction_System_Int32_

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html

1 Like

This is old but I love you.