How can I tell which key or joystick button was pressed?

Hello, everyone!

I have a question about how to make an “Options > Controls” screen like most games include. My game currently accepts a mouse, touch, keyboard, or joystick. Then the user can choose the controller s/he prefers and everything works fine, but I wish to allow him/her to tell which key on the keyboard or Joystick button s/he wants to perform each action.

I don’t know any way to do that! How to tell which key/joystick button the player has pressed, so I can update the control preferences in the game?

There’s some stuff that just landed related to this.

2 Likes

“Rebind”, that’s the word I was looking for ^^’

Thank you for the content, it surely will help a lot! :slight_smile:

UPDATE:
It is really simple to rebind a key with that! While the video and Unity samples help a lot, I want to share this very simple code snippet I managed to work:

InputAction action = myPlayerInput.actions.FindAction("MyAction");
action.Disable();

var rebinder = action.PerformInteractiveRebinding(/* binding index to override */)
.OnComplete(operation => {
    action.Enable();
    Debug.Log("Rebind completed!");
    operation.Dispose();
})
.OnCancel(operation => {
    action.Enable();
    Debug.Log("Rebind cancelled!");
    operation.Dispose();
});

rebinder.Start();
Debug.Log("Waiting for input...");

*Notice it is a very very simplified snippet. For more complex examples, follow Rene-Damm link.

1 Like