How can I recognize a device used for input?

I’m using the new input system.

When playing with the mouse, I have the cursor change frequently for various contexts. When playing with the gamepad, I want the cursor to disappear so it isn’t in the way.

I have code to make the cursor disappear, but how can I tell if the player is using a particular kind of device?
A player could have both devices connected; in fact I have both connected when I test my game, and might use one or the other.

How can I tell if a command is being called by the keyboard versus the gamepad?

1 Like

The CallbackContext has a .control property (type InputControl) which in turn has a .device property, so you can check whether the associated device is of a certain type, e.g. is Gamepad

When you’re executing any actions you get the InputAction.CallbackContext parameter.
You can read its control property.
This control property has a property called device which identifies the device the control used when executed said action.

Edit: And @BillyWM was faster. :smile:

Reading the New Input documentation is like eating a milkshake made with cement mix.

How do I actually use this in context?
My “OnMove” function has “InputValue value” as an argument, but I can’t get a callbackContext out of it.

How do I get a reference to InputAction.CallbackContext?
It doesn’t look like it gets passed to the “On(action)” commands that get called when a player hits a button.

Right, I’d been using the “Invoke Unity Events” behavior and had forgotten to mention: when you choose “Send Messages” as the “behavior” on PlayerInput instead of Invoke Unity Events you get InputValue instead of CallbackContext. The docs say this is a wrapper around CallbackContext but I can’t see a way to drill down into the CallbackContext, and InputValue doesn’t seem terribly useful. Seems like if you want to ever stray off the ‘happy path’ it’s not useful.

You’ll need to change the dropdown to Invoke Unity Events, wire up each callback under the events section, and change the function signature so it’s like OnSomething(InputAction.CallbackContext context)