Android: enumerating the buttons on a gamepad
Asked Answered
M

1

7

I have an Android device into which a gamepad can be connected (it's a totally standard XBox 360 controller, but I need to support other varieties as well). The gamepad is showing up as a InputDevice with some analogue axes and some buttons.

I need to be able to query Android to find out what buttons the gamepad supports. Does anyone know how to do this?

I know that the system has this information, because if I write a command-line app which opens /dev/input/event... and queries the buttons using the EVIOCGBIT ioctl, I get a nice list of supported buttons from the kernel. But I can't do this from an Android application because I don't have permission to access the input devices directly.

It looks like it should be possible to get the InputDevice's KeyCharacterMap object and query that; but it looks like Android has attached the default QWERTY keyboard keymap to the gamepad, rather than constructing one that actually matches what the gamepad supports. This will happily tell me that the gamepad has a Q key, which it doesn't, and that it doesn't have a BUTTON_X key, which it does, and for which I am receiving key events. So that's not helping.

Is there any way to do this?

Mathre answered 27/7, 2012 at 11:18 Comment(2)
There should be documentation from your vendor on this. What device are you using?Lamed
Any --- this needs to be a portable solution.Mathre
M
3

I haven't figured out a way yet, but I have found a horrible workaround.

If you call KeyCharacterMap.deviceHasKey(keycode), I can ask Android if any input device on the system supports the specified keycode. By iterating through all possible buttons that can occur on a gamepad (usefully, they're all called KeyEvent.KEYCODE_BUTTON_something), I can figure out that any connected gamepad must support at least some of these buttons.

It's not a very nice solution --- if I have an XBox 360 controller and a Wiimote connected at the same time, then I can't figure out that the XBox controller does not have buttons 1 or 2, and the Wiimote does not have X or Y, for example. But at least it's a start.

If anyone has any better ideas, please suggest some...

Mathre answered 30/7, 2012 at 9:18 Comment(1)
It looks like they finally added this in API level 19 (4.4) with InputDevice.hasKeys: developer.android.com/reference/android/view/…Bethlehem

© 2022 - 2024 — McMap. All rights reserved.