Android Input之JoyStick

来源:互联网 发布:阿里巴巴淘宝城 编辑:程序博客网 时间:2024/06/05 02:54

一、配置文件

system/usr/keylayout/Generic.kl

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ......  
  2. # Joystick and game controller axes.  
  3. # Axes that are not mapped will be assigned generic axis numbers by the input subsystem.  
  4. axis 0x00 X  
  5. axis 0x01 Y  
  6. axis 0x02 Z  
  7. axis 0x03 RX  
  8. axis 0x04 RY  
  9. axis 0x05 RZ  
  10. axis 0x06 THROTTLE  
  11. axis 0x07 RUDDER  
  12. axis 0x08 WHEEL  
  13. axis 0x09 GAS  
  14. axis 0x0a BRAKE  
  15. axis 0x10 HAT_X  
  16. axis 0x11 HAT_Y  

二、原理部分

ics/frameworks/base/core/Java/Android/view/ViewRootImpl.java

添加对axis 0x03 RX和axis 0x04 RY左边做上下左右按键的支持:

1.调用关系如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) {  
  2.   ......  
  3. }  
  4. private void dispatchGenericMotion(MotionEvent event, boolean sendDone) {  
  5.   ......  
  6. }  
  7.   
  8. public void handleMessage(Message msg) {  
  9.   ......  
  10. }  
  11. private void deliverGenericMotionEvent(MotionEvent event, boolean sendDone) {  
  12.   ......  
  13. }  

2.核心代码片段

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private void updateJoystickDirection(MotionEvent event, boolean synthesizeNewKeys) {  
  2.   ......  
  3.   int xDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_HAT_X));  
  4.   if (xDirection == 0) {  
  5.     xDirection = joystickAxisValueToDirection(event.getX());  
  6.   }  
  7.   //add by tankai  
  8.   if (xDirection == 0) {  
  9.     xDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_RX));  
  10.   }  
  11.   //end  
  12.   int yDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_HAT_Y));  
  13.   if (yDirection == 0) {  
  14.     yDirection = joystickAxisValueToDirection(event.getY());  
  15.   }  
  16.   //add by tankai  
  17.   if (yDirection == 0) {  
  18.     yDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_RY));  
  19.   }  
  20.   //end  
  21.   if (xDirection != mLastJoystickXDirection) {  
  22.     if (mLastJoystickXKeyCode != 0) {  
  23.       deliverKeyEvent(new KeyEvent(time, time,  
  24.                         KeyEvent.ACTION_UP, mLastJoystickXKeyCode, 0, metaState,  
  25.                         deviceId, 0, KeyEvent.FLAG_FALLBACK, source), false);  
  26.       mLastJoystickXKeyCode = 0;  
  27.     }  
  28.     mLastJoystickXDirection = xDirection;  
  29.     if (xDirection != 0 && synthesizeNewKeys) {  
  30.       mLastJoystickXKeyCode = xDirection > 0  
  31.                         ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT;  
  32.       deliverKeyEvent(new KeyEvent(time, time,  
  33.                         KeyEvent.ACTION_DOWN, mLastJoystickXKeyCode, 0, metaState,  
  34.                         deviceId, 0, KeyEvent.FLAG_FALLBACK, source), false);  
  35.     }  
  36.   }  
  37.   
  38.   if (yDirection != mLastJoystickYDirection) {  
  39.     if (mLastJoystickYKeyCode != 0) {  
  40.       deliverKeyEvent(new KeyEvent(time, time,  
  41.                         KeyEvent.ACTION_UP, mLastJoystickYKeyCode, 0, metaState,  
  42.                         deviceId, 0, KeyEvent.FLAG_FALLBACK, source), false);  
  43.       mLastJoystickYKeyCode = 0;  
  44.     }  
  45.     mLastJoystickYDirection = yDirection;  
  46.     if (yDirection != 0 && synthesizeNewKeys) {  
  47.       mLastJoystickYKeyCode = yDirection > 0  
  48.                         ? KeyEvent.KEYCODE_DPAD_DOWN : KeyEvent.KEYCODE_DPAD_UP;  
  49.       deliverKeyEvent(new KeyEvent(time, time,  
  50.                         KeyEvent.ACTION_DOWN, mLastJoystickYKeyCode, 0, metaState,  
  51.                         deviceId, 0, KeyEvent.FLAG_FALLBACK, source), false);  
  52.     }  
  53.   }  
  54. }  
0 0
原创粉丝点击