Question

I have to work with the InputDevice.getSources() method to determine the type (source) of InputDevice. But instead of returning a predetermined integer, it returns a combined bitfield, for example: 16786707 (this is an actual value from my gamepad).

As you can see 16786707 is not listed in the InputDevice documentation page because it is generated on the fly. How do I parse the number 16786707 to determine whether the InputDevice is: a SOURCE_CLASS_JOYSTICK (16), or a SOURCE_GAMEPAD (1025), or SOURCE_JOYSTICK (16777232). My goal is to determine in a initialization method if the InputDevice is a gamepad like the Xbox 360 gamepad or any other gamepad.

Was it helpful?

Solution

                bytes          int        name
-------------------------------------------------------------
1000000000010010100010011  //16786707 <- Device
                    10000  //16       <- SOURCE_CLASS_JOYSTICK
              10000000001  //1025     <- SOURCE_GAMEPAD
1000000000000000000010000  //16777232 <- SOURCE_JOYSTICK

The device is a GamePad, a Joystick, and a class_joystick (guess that means its a joystick).

You'll need to use AND to check what:

int device = 16786707;
boolean is_source_class_joystick = ((device & SOURCE_CLASS_JOYSTICK) == SOURCE_CLASS_JOYSTICK);
boolean is_source_gamepad = ((device & SOURCE_GAMEPAD) == SOURCE_GAMEPAD);
boolean is_source_joystick = ((device & SOURCE_JOYSTICK) == SOURCE_JOYSTICK);

This should work.

Edit: I also checked, it can also be considered a keyboard and a mouse.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top