Android Input之JoyStick

一、配置文件

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());
  }
  //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);
    }
  }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值