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.

Resolved Determine currently used device type !?

Discussion in 'Input System' started by Vagabond_, Jan 14, 2021.

  1. Vagabond_

    Vagabond_

    Joined:
    Aug 26, 2014
    Posts:
    1,101
    Hi,

    @Rene-Damm could you please help with this one !?

    TLDR; we need a way to detect button on what device was just pressed in case player can use keyboard and gamepad at the same time !

    So let's say i have a keyboard and an XBox controller. Our game has a partial controller support and we are able to navigate part of the UI using the keyboard/gamepad. If i press a keyboard button i would like to show the correct icons on screen and if i press any control on the gamepad i would want to show appropriate navigation icons on screen.

    So how i would detect the currently used device - e.g. if a button on the XBox controller or on the Keyboard was pressed !?

    We can do this with ease using InControl but have no idea how to do it properly using the new input system !

    Looking into this thread, we are only able to get it working when a device is unplugged and control is moved to another device !
     
    reza_mirzaei likes this.
  2. sodarocketstudios

    sodarocketstudios

    Joined:
    Jan 14, 2021
    Posts:
    20
    If you create control schemes, you can determine the current control scheme through the PlayerInput class. you can use that to determine which device is being used.

    https://docs.unity3d.com/Packages/c...Plugins_PlayerInput_PlayerInput_controlScheme

    You get to set all the devices you want associated with each control scheme. just be sure to have both the keyboard and mouse included in the list.

    I have a video about the input system and I briefly discuss control schemes here
    .
     
  3. Vagabond_

    Vagabond_

    Joined:
    Aug 26, 2014
    Posts:
    1,101
    Hi, thanks, your video tutorial is really great !

    However about changing controls, it actually wasn't working because i had the input action asset set in a way where i had a gamepad scheme added as optional to the keyboard one. Removing this and leaving only the keyboard as required for "Keyboard" and gamepad for "Gamepad" fixed it.

    upload_2021-1-15_10-18-4.png
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,807
    If you (can) handle the said Action yourself, the CallbackContext has a member called
    control
    , which is an InputControl and it has a
    device
    which should be the device initiated the Action.
     
    bakosd likes this.
  5. datagreed

    datagreed

    Joined:
    Sep 17, 2018
    Posts:
    42
    @Lurking-Ninja how do you use InputControl.device to determine if it's gamepad or mouse? I can only see it giving some generic string description. I could theoretically parse it, but in my case it even contains a typo: `XboxOneGampadMacOSWireless:/XboxOneGampadMacOSWireless` - so it does not seem reliable, I don't see any enum listing types of devices to compare it against.

    The other weird thing is that sendMessages and UnityEvents on PlayerInput have different signatures. One expects CallbackContext and another expects some design abomnination called InputAction that hides access to the actual context. The new player input system is such a poorly documented mess its not even funny at this point.
     
  6. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    67
    Code (CSharp):
    1. if (device is Gamepad)
    2.     Debug.Log("Gamepad");
    3. else if (device is Mouse)
    4.     Debug.Log("Mouse");
    PlayerInput indeed has been a bit of a misadventure.
     
  7. datagreed

    datagreed

    Joined:
    Sep 17, 2018
    Posts:
    42

    Thanks, I did not realize it's that simple, although casting types so often (almost every frame) seems like a bit of heavy lifting