一、配置文件
system/usr/keylayout/Generic.kl
- ......
- # Joystick and game controller axes.
- # Axes that are not mapped will be assigned generic axis numbers by the input subsystem.
- axis 0x00 X
- axis 0x01 Y
- axis 0x02 Z
- axis 0x03 RX
- axis 0x04 RY
- axis 0x05 RZ
- axis 0x06 THROTTLE
- axis 0x07 RUDDER
- axis 0x08 WHEEL
- axis 0x09 GAS
- axis 0x0a BRAKE
- axis 0x10 HAT_X
- axis 0x11 HAT_Y
二、原理部分
ics/frameworks/base/core/Java/Android/view/ViewRootImpl.java
添加对axis 0x03 RX和axis 0x04 RY左边做上下左右按键的支持:
1.调用关系如下:
- public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) {
- ......
- }
- private void dispatchGenericMotion(MotionEvent event, boolean sendDone) {
- ......
- }
-
- public void handleMessage(Message msg) {
- ......
- }
- private void deliverGenericMotionEvent(MotionEvent event, boolean sendDone) {
- ......
- }
2.核心代码片段
- private void updateJoystickDirection(MotionEvent event, boolean synthesizeNewKeys) {
- ......
- int xDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_HAT_X));
- if (xDirection == 0) {
- xDirection = joystickAxisValueToDirection(event.getX());
- }
-
- if (xDirection == 0) {
- xDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_RX));
- }
-
- int yDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_HAT_Y));
- if (yDirection == 0) {
- yDirection = joystickAxisValueToDirection(event.getY());
- }
-
- if (yDirection == 0) {
- yDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_RY));
- }
-
- if (xDirection != mLastJoystickXDirection) {
- if (mLastJoystickXKeyCode != 0) {
- deliverKeyEvent(new KeyEvent(time, time,
- KeyEvent.ACTION_UP, mLastJoystickXKeyCode, 0, metaState,
- deviceId, 0, KeyEvent.FLAG_FALLBACK, source), false);
- mLastJoystickXKeyCode = 0;
- }
- mLastJoystickXDirection = xDirection;
- if (xDirection != 0 && synthesizeNewKeys) {
- mLastJoystickXKeyCode = xDirection > 0
- ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT;
- deliverKeyEvent(new KeyEvent(time, time,
- KeyEvent.ACTION_DOWN, mLastJoystickXKeyCode, 0, metaState,
- deviceId, 0, KeyEvent.FLAG_FALLBACK, source), false);
- }
- }
-
- if (yDirection != mLastJoystickYDirection) {
- if (mLastJoystickYKeyCode != 0) {
- deliverKeyEvent(new KeyEvent(time, time,
- KeyEvent.ACTION_UP, mLastJoystickYKeyCode, 0, metaState,
- deviceId, 0, KeyEvent.FLAG_FALLBACK, source), false);
- mLastJoystickYKeyCode = 0;
- }
- mLastJoystickYDirection = yDirection;
- if (yDirection != 0 && synthesizeNewKeys) {
- mLastJoystickYKeyCode = yDirection > 0
- ? KeyEvent.KEYCODE_DPAD_DOWN : KeyEvent.KEYCODE_DPAD_UP;
- deliverKeyEvent(new KeyEvent(time, time,
- KeyEvent.ACTION_DOWN, mLastJoystickYKeyCode, 0, metaState,
- deviceId, 0, KeyEvent.FLAG_FALLBACK, source), false);
- }
- }
- }