Capturing game controller button events in Processing for Android

Hello everyone!

I’m trying to write a sketch in Processing for Android that gets inputs from a physical game controller.

With help from the documentation on controller input I managed to make a sketch that uses android.view.InputDevice and makes a list of connected controllers and outputs it to the console.

Now I am trying to capture button presses and I am finding this challenging.

Here’s the code I have cobbled together, focusing on the directional keys for now.

Dpad dpad = new Dpad();

public boolean onKeyDown(int keyCode, KeyEvent event) {
    int direction = dpad.getDirectionPressed(event);
    // if the event comes from a dpad
    if (direction != -1) {
        // check which direction was pressed
        switch (direction) {
            case Dpad.UP:
                System.out.println("UP button pressed");
                break;
            case Dpad.LEFT:
                System.out.println("LEFT button pressed");
                break;
            case Dpad.RIGHT:
                System.out.println("RIGHT button pressed");
                break;
            case Dpad.DOWN:
                System.out.println("DOWN button pressed");
                break;
            default:
                break;
        }
    }
    return true;
}

Note: The Dpad class comes from this example in the documentation.

The code snippet below comes from the doc as well. It shows that the onKeyDown method should be an Override inside of a class that extends the View :face_with_spiral_eyes:

public class GameView extends View {
    ...

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         ...

This is where I’m getting stumped as these concepts don’t really fit with my understanding of Processing.

How can I capture these gamepad events in Processing for Android?

1 Like

Disclaimer: I’ve never dealt w/ Android coding. This is purely blind advice! :dark_sunglasses:

  • In Java everything must be inside a class or interface.
  • Processing hides that basic truth from us when our code is inside a “.pde” file.
  • But if we check the “.java” file which got transpiled out of our “.pde” files we’ll realize everything ends up wrapped inside a PApplet subclass after all.
  • When we need to define a callback function in Java, that also ends up belonging to some class or interface.
  • So even callbacks gotta belong to some datatype and all of its inherited datatypes.
  • And consequently we need to pass an instance of a callback’s class instead of the callback itself.
  • Unless a callback belongs to a @FunctionalInterface; in which case we can alternatively pass a lambda expression instead.
  • According to Android’s documentation, we’re advised to create a helper class w/ suggested name Dpad.
  • Plus a subclass which extends class View w/ suggested name GameView, in which we can @Override View’s callback functions.
  • Basically that’s where you should define your customized onKeyDown() callback event method aKa listener.
  • Unfortunately that’s where I can’t help you anymore! :cry:
  • It seems like parent class View is merely a placeholder for listener callback methods.
  • Instantiating a new View doesn’t magically activate its listeners at all.
  • You’d still need to pass its instance to some “unknown” Android method which will then register & start listening to the View’s callbacks within your sketch app.
2 Likes

Thanks for the pointers @GoToLoop :handshake: Sounds like I need to read up on Java callbacks.

There is an example about sensors in the Processing for Android documentation that shows how to get accelerometer data. It creates a android.hardware.SensorManager called manager and registers the listener with manager.registerListener(...).

I did a bit more research and it looks like Android has a method called android.hardware.input.InputManager.registerInputDeviceListener() and I’m guessing that’s the “unknown” Android method you hinted at?

Either way I have my work cut out for me! Thanks for your help :hugs:

1 Like

I am still investigating this, but it is a challenge for me since I have no prior experience with the Android framework :smiling_face_with_tear:

In the meantime I have made a feature request on the Processing for Android repository:

1 Like

Hi Sir
Maybe I am far about your topic
But Easy way to do it

1- If the controller connect via Bluetooth I think it’s easy to capture the key’s code as array and use it in sketch

2-You can read Bluetooth key code if you want using Serial Bluetooth Terminal App for Android

3 -for USB I found one library support USB for Android make connection with Arduino (not all kinds of Arduino) I don’t know if it’s support game controller

Here is example to receive Bluetooth

Note: I have posted an updated version of this question on Stackoverflow.

2 Likes

For Reference I’ll put the response I got from StackOverflow user Morrison Chang:

So it looks like there is no way around this that don’t involve customising the core of Processing for Android. Based on the 2014 issue mentioned above, It looks like Andrés Colubri (who maintains Processing for Android) was open to a pull request adding gamepad support:

I’ll see if I can wrap my head around the Android framework well enough to make that pull request. Wish me luck (or if you are able to help, please let me know!)

1 Like