Detect if Gamepad button was pressed

Hi! Im on making control hints in a main menu, and i want to detect if that a keyboard or gamepad input to show proper buttons images (eg: player uses a keyboard see like “hit enter to start”, or “press START” if any gamepad were used)

I detect a Keyboard like this:

 private void OnGUI()
    {
        if (Event.current.isKey && Event.current.type == EventType.KeyDown)
        {
            isKeyboard = true;
        }
}

but how to detect a Joystick?
I use OLD input system(!)


to save your time:
FAST ANSWER - the solution is a NEW INPUT SYSTEM.

Late in the year 2021 you should not be using OnGUI() to make your game.

OnGUI() is INCREDIBLY out of date.

OnGUI and the entire GUI subsystem was replaced by the far-more-capable UnityEngine.UI namespace back in approximately 2014 or even earlier.

You CAN still use it because the entire editor uses it, but for a built game it is the hardest most technically challenging way to do things, plus it will never be as performant as UnityEngine.UI.

I suggest working through some UnityEngine.UI tutorials, and also working through some basic input system tutorials for gamepads. Youtube has thousands of videos for how to get both of those things working.

To expand on above, you should not use OnGUI for anything other than generating an IMGUI. It is not intended for anything else, and can be called multiple times per frame, which is not good for capturing and acting on user input.

There’s no real good way to do that. by the way. Check out the almost last post in this thread for a not-so-great solution though,
https://discussions.unity.com/t/600492

I guess that’s one reason why Input is now the “old” input system. :smile:

Yeah, I tried some workaround and… just swapped to the new Input System because making this functionality with the old one is a pain in the …))) and very bad optimized, at least the way I did it.

Now it’s simple as that

    private void LateUpdate()
    {
        var gamepad = Gamepad.current.lastUpdateTime;
        var keyboard = Keyboard.current.lastUpdateTime;
        isKeyboard = (keyboard > gamepad) ? true : false;

    }

Then I just start coroutine that checks every 500ms what is the current input to show a proper control hints on a screen