No key / motion events triggered from connected game controller in Android Activity

36 Views Asked by At

I am developing a game for Android. Want to add game controller support.I read documentation how to add it. https://developer.android.com/develop/ui/views/touch-and-input/game-controllers, but nothing works. I implemented the following callback methods in my Activity:

dispatchKeyEvent(android.view.KeyEvent) dispatchGenericMotionEvent(android.view. MotionEvent)

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        boolean handled = false;
        if ((event.getSource() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
        {
            if (event.getRepeatCount() == 0)
            {
                int c = 20;
            }
            if (handled)
            {
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

They are not triggered.

The only method that is called is : enter image description here


    @Override
    public void onInputDeviceAdded(int deviceId)
    {
        int a = deviceId;
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();

        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
            || ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)
        )
        {
            // check this is input device in controller
        }
    }

So, I am sure that controller is connected, I see its device ID.

In documentation it is written that corresponding methods can be overwritten in the View. So, I moved my listeners there,the result is the same.

In the documentation .zip example was mentioned. https://android.googlesource.com/platform/development/+/refs/heads/main/samples/ControllerSample

I downloaded it and the result is the same.

I read that focus state is important for listening events. When I moved my callbacks in View I setFocuble(true).

Before, asking this question, I looked through the stack overflow, and found some issues DPAD events, but people who asked that questions receive any events from the controller. In my case, no events at all.

Project details:

minSdk 29 targetSdk 34

0

There are 0 best solutions below