[SOLVED] ActionListener: onAction called for no reason

I recently changed to LWJGL 3 because of an issue with my gamepad, but now I found a new problem.

import com.jme3.app.SimpleApplication;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.JoyAxisTrigger;
import com.jme3.input.controls.JoyButtonTrigger;
import com.jme3.system.AppSettings;

public class JoyTest extends SimpleApplication implements ActionListener {

    public static void main(String... args) {
        AppSettings settings = new AppSettings(true);
        settings.setFullscreen(false);
        settings.setUseJoysticks(true);

        JoyTest app = new JoyTest();

        app.setShowSettings(false);
        app.setSettings(settings);
        app.start();
    }

    private void initInputCommands() {
        inputManager.addMapping("left", new JoyAxisTrigger(0, 0, true));
        inputManager.addMapping("right", new JoyAxisTrigger(0, 0, false));
        inputManager.addMapping("up", new JoyAxisTrigger(0, 1, true));
        inputManager.addMapping("down", new JoyAxisTrigger(0, 1, false));
        inputManager.addMapping("button A", new JoyButtonTrigger(0, 0));

        inputManager.addListener(this, "left", "right", "up", "down"
//                ,"button A"
        );
    }

    @Override
    public void simpleInitApp() {
        initInputCommands();
    }

    @Override
    public void onAction(String name, boolean isPressed, float tpf) {
        if (isPressed) {
            System.out.println("Pressed: " + name);
        } else {
            System.out.println("Released: " + name);
        }
    }
}

The code works as expected, the method onAction is called once whenever a direction is “pressed” or “released” on the left stick. However, if I uncomment the line 29 ( , button A ), the code behaves strangely. It calls onAction endlessly, no matter if the button is being pressed or not on the controller.

What is the console output? What buttons get pressed exactly?

With the above code, if you press and release up once on your controller the output is:

Pressed: up
Released: up

After uncommenting the commented line, the expected behavior when you press and release the A button once should be:

Pressed: button A
Released: button A

Instead, what it actually shows is something like:

Released: button A
Released: button A
Released: button A
Released: button A
Released: button A
Released: button A
Released: button A
Released: button A
Released: button A

It’s a bug – probably on the class InputManager or GlfwJoystickInput. I’m still looking for it, but I brought it here because I thought that “given enough eyeballs, all bugs are shallow” (Linus’s law). :grin:

If that button on the joystick is pressure sensitive then it might behave like an axis instead. That’s the only non-bug thing I can think of that might cause this.

…then again, 0, 0 seems like a coincidence that could easily tie to a bug I guess.

The thing is that it is happening to regular buttons. That was not the behavior on LWJGL 2. The method onAction() should be called only when the state of a button changes (from not pressed to pressed and vice-versa).

I’ve found that the problem was on the GlfwJoystickInput class. I fixed and PRed.

2 Likes

May I ask what is your gamepad, if it was working on DirectInput or XInput, and what was the issue?

I have a Logitech Gamepad F710 and, because the start button wasn’t working on both Direct Input and XInput modes, I was told to change to LWJGL 3. I did the change and now all the buttons on my gamepad work.

I found another issue though: the onAction() method of the ActionListener interface was being called on every cycle, no matter if a button was pressed or not. It was caused by a problem on the GlfwJoystickInput class, which I fixed and PRed.

Damn!

So I guess that if I want to have decent support to joypads I’ll need to upgrade as well?