How to check the device type of the last input

Hey guys, I’m looking for a way to switch my Input handling code depending on the type of controller you’re using. If you’re using Mouse&Keyboard I want to handle input for example using void MouseLook(Vector2) method, and with Gamepad, I want to use void GamepadLook(Vector2) , and I want the player to be able to switch inputs seamlessly, so I need to know the last input’s controller type.

What’s the best way to do this?

In general, the recommended way is to use control schemes. I.e. to add one control scheme for mouse&keyboard and one for gamepad (similar to how it’s done with the default actions you get when you click the “Create Actions…” button in the PlayerInput component inspector).

With this, you can query the current control scheme from PlayerInput and receive a notification when the user switches from one control scheme to another.

@Rene-Damm I am actually already using Control Schemes, one for Gamepad and one for Keyboard&Mouse, however I am not using the PlayerInput component, instead I’m using the generated C# class directly

One way I’ve been able to figure out how to do it is by subscribing to the performed event of each Action. So I have a MouseLook action and a GamepadLook action.

void Awake()
{
    _actions = new MyActions();
    _actions.Player.GamepadLook.performed += OnGamepadLookPerformed;
    _actions.Player.MouseLook.performed += OnMouseLookPerformed;
}

void OnMouseLookPerformed(InputAction.CallbackContext context)
{
    _currentControllerType = ControllerType.MouseKeyboard;
}

void OnGamepadLookPerformed(InputAction.CallbackContext context)
{
    _currentControllerType = ControllerType.Gamepad;
}

But maybe it’s not the most efficient way of doing it as this method gets called many times over and over again every time you perform the action, is there any other way?

1 Like