Search Unity

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,148
    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_b_mirzaei likes this.
  2. sodarocketstudios

    sodarocketstudios

    Joined:
    Jan 14, 2021
    Posts:
    21
    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,148
    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. 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.
     
    Meatloaf4 and bakosd like this.
  5. datagreed

    datagreed

    Joined:
    Sep 17, 2018
    Posts:
    44
    @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:
    116
    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:
    44

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

    NovaUnity101

    Joined:
    Oct 17, 2021
    Posts:
    1
    I found a good solution, just make a, "PlayerInput" variable in your script and name it something like myPlayerInput. From there you can get the name of the current control scheme as a string using, "myPlayerInput.controlScheme". So if your control scheme is named PC or Keyboard or whatever, you can check for that!

    bool usingControllerForInput = playerInput.currentControlScheme != "PC";
     
    Starwitch and Awufumana like this.
  9. Terazilla

    Terazilla

    Joined:
    Apr 27, 2013
    Posts:
    20
    Man, finding this thread is wild. Determining what device is currently being used is so incredibly basic and yet none of these solutions are good. Like I need to be able to tell the difference between a PS4 gamepad, PS5 gamepad, and an Xbox One gamepad, am I supposed to set up like ten basically-identical ControlSchemes to work around this?

    It seems like InputDevice description is supposed to be able to do this job, but it also seems basically broken with none of the fields filled in, and doesn't even have a basic "gamepad" "keyboard" "mouse" etc enum or something to key off of. One of the fields has a bunch of JSON or something in it I guess I could parse through?
     
    wifitofu likes this.
  10. RiverExplorer

    RiverExplorer

    Joined:
    Jul 28, 2021
    Posts:
    21
    This does not work for me. It *ALWAYS* returns "generic xr device". Even when on the PC with keyboard.