0 votes

I want to make a module that could determine pressing the volume buttons Android devices. Although I do not know java, but I found the code that I need on the stackoverflow:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_DOWN) {
                //TODO
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                //TODO
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
    }
}

Then I created an application in android studio that works successfully on my device and determines the volume buttons.
Java code from the application:

package com.test.exampleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView myTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myTextView = (TextView)findViewById(R.id.myTextView);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {
                    myTextView.setText("Volume up pressed!");
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (action == KeyEvent.ACTION_DOWN) {
                    myTextView.setText("Volume down pressed!");
                }
                return true;
            default:
                return super.dispatchKeyEvent(event);
        }
    }
}

Then I tried to convert this code into code for the module:

package org.godotengine.godot;

import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;

public class GodotVolumeButtons extends Godot.SingletonBase {

    private Activity activity = null; // The main activity of the game
    private int instance_id = 0;

    public void set_id(int instance_id)
    {
        this.instance_id = instance_id;
        Log.d("godot", "GodotVolumeButtons: set_id");
    }

    // @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {
                    Log.d("godot", "Volume down pressed!");
                    GodotLib.calldeferred(instance_id, "_on_volume_up_pressed", new Object[] { });
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (action == KeyEvent.ACTION_DOWN) {
                    Log.d("godot", "Volume down pressed!");
                    GodotLib.calldeferred(instance_id, "_on_volume_down_pressed", new Object[] { });
                }
                return true;
            default:
                return dispatchKeyEvent(event);
        }
    }

    /* Definitions
     * ********************************************************************** */

    /**
     * Initilization Singleton
     * @param Activity The main activity
     */
    static public Godot.SingletonBase initialize(Activity activity) {
        return new GodotVolumeButtons(activity);
    }

    /**
     * Constructor
     * @param Activity Main activity
     */
    public GodotVolumeButtons(Activity activity) {
        registerClass("GodotVolumeButtons", new String[] { "set_id" });
        this.activity = activity;
    }
}

With this code, export templates are compiled without errors. When debugging, this module is defined, but the calldeferred does not work.
Please tell me, what did I do wrong, or what did not I do?

asked Mar 4, 2018 in Engine by iroman (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.