How to intercept Button press on Android

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SilvanaBanana

Hello, I am currently trying to invoke callback methods when the Volume Keys or a Hardware Key (Back, Home, Overview ) is pressed. Therefore, I started to implement a custom android module but atm, I am wondering how I can override these methods as they are part of the Activity class and not of GodotSingleton.

Is there a way to intercept presses of the Volume/SystemUI Buttons and execute my own code? I want something like this:

public class GodotInputHandling extends Godot.SingletonBase {
    private static final String TAG = "inputHandling_Activity";

    private Activity activity;
    private int instanceId = 0;

    static public Godot.SingletonBase initialize(Activity p_activity)
    {
        return new GodotInputHandling(p_activity);
    }

    public GodotInputHandling(Activity p_activity)
    {
        registerClass("GodotInputHandling", new String[]
            {
                "initModule",
            });
        activity = p_activity;
    }

    public void initModule(int _instanceId) {
        //some init code here if needed
    }

    //I want something like this, but it should override the metod in the activity
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        //check which key it was and call method in gdscript like:
        // if(event == .... ){
        //     GodotLib.calldeferred(instance_id, "_on_volume_down_pressed", new Object[] { });
        // } else {
        //      GodotLib.calldeferred(instance_id, "_on_foo_pressed", new Object[] { });
        // }

    }

    //Or something like this, but this method is also part of the activity
    @Override
    public boolean onKeyDown(int keyCode) {
        //...
    }

}