@Override @TargetApi(12) public boolean onGenericMotionEvent(MotionEvent event) { // Log.d(TAG, "onGenericMotionEvent: " + event); if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) { if (Build.VERSION.SDK_INT >= 12) { InputDeviceState state = getInputDeviceState(event); if (state == null) { Log.w(TAG, "Joystick event but failed to get input device state."); return super.onGenericMotionEvent(event); } state.onJoystickMotion(event); return true; } } if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { switch (event.getAction()) { case MotionEvent.ACTION_HOVER_MOVE: // process the mouse hover movement... return true; case MotionEvent.ACTION_SCROLL: NativeApp.mouseWheelEvent(event.getX(), event.getY()); return true; } } return super.onGenericMotionEvent(event); }
// We grab the keys before onKeyDown/... even see them. This is also better because it lets us // distinguish devices. @Override public boolean dispatchKeyEvent(KeyEvent event) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1 && !isXperiaPlay) { InputDeviceState state = getInputDeviceState(event); if (state == null) { return super.dispatchKeyEvent(event); } // Let's let back and menu through to dispatchKeyEvent. boolean passThrough = false; switch (event.getKeyCode()) { case KeyEvent.KEYCODE_BACK: case KeyEvent.KEYCODE_MENU: passThrough = true; break; default: break; } // Don't passthrough back button if gamepad. int sources = event.getSource(); switch (sources) { case InputDevice.SOURCE_GAMEPAD: case InputDevice.SOURCE_JOYSTICK: case InputDevice.SOURCE_DPAD: passThrough = false; break; } if (!passThrough) { switch (event.getAction()) { case KeyEvent.ACTION_DOWN: if (state.onKeyDown(event)) { return true; } break; case KeyEvent.ACTION_UP: if (state.onKeyUp(event)) { return true; } break; } } } // Let's go through the old path (onKeyUp, onKeyDown). return super.dispatchKeyEvent(event); }
// We simply grab the first input device to produce an event and ignore all others that are // connected. @TargetApi(Build.VERSION_CODES.GINGERBREAD) private InputDeviceState getInputDeviceState(InputEvent event) { InputDevice device = event.getDevice(); if (device == null) { return null; } if (inputPlayerA == null) { inputPlayerADesc = getInputDesc(device); Log.i(TAG, "Input player A registered: desc = " + inputPlayerADesc); inputPlayerA = new InputDeviceState(device); } if (inputPlayerA.getDevice() == device) { return inputPlayerA; } if (inputPlayerB == null) { Log.i(TAG, "Input player B registered: desc = " + getInputDesc(device)); inputPlayerB = new InputDeviceState(device); } if (inputPlayerB.getDevice() == device) { return inputPlayerB; } if (inputPlayerC == null) { Log.i(TAG, "Input player C registered"); inputPlayerC = new InputDeviceState(device); } if (inputPlayerC.getDevice() == device) { return inputPlayerC; } return inputPlayerA; }