一、配置文件

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 Xaxis 0x01 Yaxis 0x02 Zaxis 0x03 RXaxis 0x04 RYaxis 0x05 RZaxis 0x06 THROTTLEaxis 0x07 RUDDERaxis 0x08 WHEELaxis 0x09 GASaxis 0x0a BRAKEaxis 0x10 HAT_Xaxis 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());  }  //add by tankai  if (xDirection == 0) {    xDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_RX));  }  //end  int yDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_HAT_Y));  if (yDirection == 0) {    yDirection = joystickAxisValueToDirection(event.getY());  }  //add by tankai  if (yDirection == 0) {    yDirection = joystickAxisValueToDirection(event.getAxisValue(MotionEvent.AXIS_RY));  }  //end  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);    }  }}

更多相关文章

  1. ScrollView源码分析
  2. android 高斯模糊
  3. Android调用系统camera
  4. Glide 圆角图片
  5. Android(安卓)Service系列(十七)发送request到后台service
  6. 探索FragmentTransaction#commit()抛出IllegalStateException
  7. Android学习笔记-Android初级 (二)
  8. Android中模拟HOME键功能
  9. android activity与view的联系--window

随机推荐

  1. Android(安卓)SystemUI之启动流程(一)
  2. Webview缓存处理
  3. 2013.07.12——— android red5安装
  4. Android(安卓)基础-1.0 按钮4种点击事件
  5. android模拟器不能上网
  6. Android(安卓)Handler的使用!!!
  7. Android中实现圆角图片的几种方法
  8. Android中获取应用程序(包)的大小-----Pa
  9. 安卓开发:学生信息管理小程序
  10. Android之对话框Dialog