Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question [New Input System] How to detect any joystick button press from code?

Discussion in 'Input System' started by pastaluego, May 5, 2023.

  1. pastaluego

    pastaluego

    Joined:
    Mar 30, 2017
    Posts:
    196
    The new input system's InputActionAsset can appropriately listen for all joystick buttons if the adapter is plugged in, but otherwise it only pre-emptively lists one stick and one 'trigger' button for all un-specified joysticks, so I'm not sure how to detect the exact button presses from any joystick at runtime if there's no way to know what adapter the person is using for their non-'gamepad' controller.

    The old Input system has JoystickButton1, JoystickButton2, JoystickButton3, etc as part of the KeyCode enum, which let you do

    Code (CSharp):
    1.  
    2. for(int i = 0; i < keycodes; i++) {
    3.     if(Input.GetKeyDown(keycodes[i]))
    4.         Debug.Log(keycodes[i] + " was pressed.");
    5. }
    6.  
    7. //prints out JoystickButton3 or JoystickButton9 etc
    8. //depending on whatever button I press on my non-"gamepad" 'Joystick'
    9. //connected via random 3rd party adapters for old controllers
    10. //such as Mcbazel PS2 to USB adapter is a 'Joystick' in the new input system,
    11.  
    to accurately detect the exact button press from any joystick.

    Is there a way to do that at runtime in the new input system? I basically just need whatever PerformInteractiveRebinding does to detect buttons except be able to just get the name of any un-mapped button pressed at runtime.
    InputSystem.onAnyButtonPress's detection doesn't work the same as PerformInteractiveRebinding or the InputActionAsset's 'Listen' detection. It for some reason produces false positives from my adapter even when no controller is plugged in, while the others don't. Even the onControlsChanged event doesn't produce these false positives. So I just need to hook into whatever method these functions are using to detect button inputs.

    Or rather is there some list of all possible joystick buttons somewhere in the new system that I could manually poll instead?
     
    Last edited: May 6, 2023