I’m looking for a way to detect any input into the game engine for the purposes of rebinding keys.
I know that the _UnhandledKeyInput override exists and that works brilliantly for keyboard inputs, however it does not detect mouse inputs nor joypad inputs.
I also know that for general use I can get the actions from the InputMap however with the purpose being binding a new key to an action, this will not work.
You could have a look at _UnhandledInput, that will catch inputs from any device, and then, use the event passed in as a parameter to detect what type of device the event came from (if you need that).
In the game I’m currently working on, I’ve done my remapping system by implementing both _Input and _UnhandledInput so that I’m sure I catch every possible input. I don’t know if my method is the right one (probably not tbh), but it seems to work fine so I’m sharing in case it can help.
My code looks like this:
public override void _Input(InputEvent evt)
{
if (IsRemapping)
TryRemapInput(evt);
}
public override void _UnhandledInput(InputEvent evt)
{
if (IsRemapping)
TryRemapInput(evt);
}
Basically, I’m redirecting any input, whether they are handled or not, to the same remap function.
Thanks for your help on this, it absolutely pointed me int he correct direction.
To sum up what I ended up with is like this.
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey || @event is InputEventMouseButton || @event is InputEventJoypadButton)
{
if (InputManager.Instance.KeybindingEntryMode && @event.IsReleased())
{
var keySet = SetCustomKeybinding(@event);
Task.Run(async () =>
{
await Task.Delay(10);
InputManager.Instance.KeybindingEntryMode = !keySet;
});
}
}
base._Input(@event);
}
Effectively _Input checks for any event, I filter to the ones I want to look at, check if it’s a key up event and whether I should be looking for key entries in the first place, set the custom key binding and then after a small delay set looking for entries to whether we actually received a valid key.
The small delay is to add some click stop on buttons, preventing my system from re-clicking the button if the pressed key is something like enter, or A on a controller.