How to detect any gamepad input and keyboard input

I want to detect whether the player is using touchscreen, keyboard or gamepad as the current input device.

For touch screen, I can use TouchCollection.AnyTouch().

For keyboard, I found in this post that I can use this line
if (Keyboard.GetState().GetPressedKeys().Length > 0)
The problem with this method is that it cannot differentiate keyboard input and gamepad input – any gamepad input will make Keyboard.GetState().GetPressedKeys().Length larger than 0.

For gamepad input, well, I found nothing…

So here is the question: is there an elegant way to detect any keyboard input and gamepad input?

Anyone knows the answer? :slightly_smiling:

The best I can suggest at the moment is to check for button pressed of every button you may use in the game. That is what we have done before. You’re not going to be using every key on the keyboard, so just check the pressed state of the keys you are interested in. You’re going to be doing this anyway in a regular input poll. We checked touch panel, keyboard, mouse and gamepad every frame. We would then set a variable to let us know which input device had any input in that frame. We used this to change some UI images to be relevant to the input device the player was using.

Thanks for the reply @KonajuGames! That is totally fine and definitely will work. It is just that it is tedious and doesn’t look as elegant as TouchCollection.AnyTouch()…

I believe to check for Gamepad input you could keep a copy of the previous frame’s GamePadState and simply see if it is different to the current state. The comparison operators are overloaded for the GamePadState class.

So something along the lines of:

GamePadState currentState = GamePad.GetState( PlayerIndex.One );
if ( previousState != currentState )
//Something was pressed (or depressed) - do whatever you need to here

This checks against the Buttons, DPad, Thumbsticks, and Triggers structs. The only sort of “gotcha” is that it also checks against the IsConnected and PacketNumber structs. So if you want to only look at actual buttons presses, you if check would have to look at those as well.

I’m unsure if there’s anything similar for Keyboard, but you could use the check you mentioned from the previous post and as long as there was no gamepad input, that means it’s keyboard input

Sorry I paid no attention whatsoever to the date on this. Came across it while searching and thought I’d try to be helpful.
Ignore me…