简体   繁体   English

如何确定android中输入设备的来源?

[英]How do I determine what is the source of Input Device in android?

I have to work with the InputDevice .getSources() method to determine the type (source) of InputDevice. 我必须使用InputDevice .getSources()方法来确定InputDevice的类型(源)。 But instead of returning a predetermined integer, it returns a combined bitfield, for example: 16786707 (this is an actual value from my gamepad). 但是,它没有返回预定的整数,而是返回一个组合的位域,例如:16786707(这是我的游戏手柄的实际值)。

As you can see 16786707 is not listed in the InputDevice documentation page because it is generated on the fly. 如您所见, InputDevice文档页面中未列出16786707,因为它是即时生成的。 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). 如何解析数字16786707以确定InputDevice是: SOURCE_CLASS_JOYSTICK (16)还是SOURCE_GAMEPAD (1025)还是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. 我的目标是在一种初始化方法中确定InputDevice是像Xbox 360游戏手柄还是任何其他游戏手柄之类的游戏手柄。

                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). 该设备是GamePad,操纵杆和class_joystick(猜测为操纵杆)。

You'll need to use AND to check what: 您需要使用AND检查以下内容:

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. 编辑:我也检查过,它也可以被认为是键盘和鼠标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM