SDL joystick events are intercepted by WebView on Android

Hello everyone. :slight_smile:

I have an Android application using SDL in which I need to use a joystick, and this application has a WebView on top of the SDL rendering.

In this application, SDL joystick events are not triggered anymore from the moment when the WebView is added.

I figured out that the joystick inputs are intercepted by the WebView itself, and I’ve been able to retrieve joystick information using the Gamepad API (https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API). But this doesn’t explain why SDL doesn’t receive the joystick events anyway.

I did some research on the web and found the hint SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS (https://wiki.libsdl.org/SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS) that could be related to my problem, but it doesn’t seem to have any impact, no joystick events are triggered. [Crying or Very sad]

Do you know what happen?

I finally found a way to solve my problem overriding some WebView methods:

Code:

import android.webkit.WebView;
import android.content.Context;
import android.view.*;
import org.libsdl.app.*;

public class CustomWebView extends WebView
{
    public CustomWebView(Context context)
    {
        super(context);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        // Dispatch the key events on the SDL canvas (including joystick buttons)
        SurfaceView surfaceView = (SurfaceView)SDLActivity.getLayout().getChildAt(0);

        return surfaceView.dispatchKeyEvent(event);
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event)
    {
        // Dispatch the motion events on the SDL canvas (including joystick axes)
        SurfaceView surfaceView = (SurfaceView)SDLActivity.getLayout().getChildAt(0);

        return surfaceView.dispatchGenericMotionEvent(event);
    }
}

I finally found a way to solve my problem overriding some WebView methods:

Code:

import android.webkit.WebView;
import android.content.Context;
import android.view.*;
import org.libsdl.app.*;

public class CustomWebView extends WebView
{
    public CustomWebView(Context context)
    {
        super(context);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        // Dispatch the key events on the SDL canvas (including joystick buttons)
        SurfaceView surfaceView = (SurfaceView)SDLActivity.getLayout().getChildAt(0);

        return surfaceView.dispatchKeyEvent(event);
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event)
    {
        // Dispatch the motion events on the SDL canvas (including joystick axes)
        SurfaceView surfaceView = (SurfaceView)SDLActivity.getLayout().getChildAt(0);

        return surfaceView.dispatchGenericMotionEvent(event);
    }
}