Search code examples
androidandroid-4.2-jelly-beanmotionevent

Gamepad Support Android


I am trying to add gamepad support to my game but I can't find anywhere how to get the motion events from a gamepad's joysticks.

I have something like this but it never seems to get called or do anything. I am testing on a XOOM with JellyBean and my gamepad works for navigating around the menus.

@Override
public boolean onGenericMotionEvent(MotionEvent e) {
    if ((e.getDevice().getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
        float x = e.getX();
        float y = e.getY();
        mJoy1.set(x, y);
        mJoy2.set(-1,1);
        mRenderer.onAxisMoved(mJoy1, mJoy2);
        return true;
    }
    return false;
}

How do I go about reading the axis data from the gamepad?


Solution

  • I was able to get it to work. I had to add these lines to the initialization of my view.

        setFocusable(true);
        setFocusableInTouchMode(true);
    

    After that I was getting the function calls.