Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can I recognize a device used for input?

Discussion in 'Input System' started by Marscaleb, Nov 25, 2021.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    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?
     
    lyeuhm likes this.
  2. BillyWM

    BillyWM

    Joined:
    Dec 29, 2018
    Posts:
    14
    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
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    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. :D
     
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    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.
     
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    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.
     
  6. BillyWM

    BillyWM

    Joined:
    Dec 29, 2018
    Posts:
    14
    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)
     
    Last edited: Nov 27, 2021