Mouse Button input not working

Hi, I made a class that used the default input manager to store states of keys/mousebuttons in a boolean array. The listeners work perfectly for keys and mousewheel but do not register mouse button presses. The strange thing is that if i press two mouse buttons at the same time and the release the ‘desired’ button as the last one it register correctly.

The code:

package Input;

import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.math.Vector2f;

/**
 *
 * @author Kuba
 */
public class InputHandler
{
    private InputManager inputManager;
    
    public static String[] MappingsStr =
    {
       "KeyUp", "KeyDown", "KeyLeft", "KeyRight", "MouseWheelUp", "MouseWheelDown", "KeyS", "KeyC", "MouseLeft", "KeyK"
    };
    
    public static enum Mappings
    {
        KeyUp, KeyDown, KeyLeft, KeyRight, MouseWheelUp, MouseWheelDown, KeyS, KeyC, MouseLeft, KeyK
    }
    
    public static boolean[] inputStates = new boolean[MappingsStr.length];
    
    public InputHandler(InputManager inputManager)
    {
        this.inputManager = inputManager;
        
        inputManager.addMapping(MappingsStr[0], new KeyTrigger(KeyInput.KEY_UP));
        inputManager.addMapping(MappingsStr[1], new KeyTrigger(KeyInput.KEY_DOWN));
        inputManager.addMapping(MappingsStr[2], new KeyTrigger(KeyInput.KEY_LEFT));
        inputManager.addMapping(MappingsStr[3], new KeyTrigger(KeyInput.KEY_RIGHT));
        inputManager.addMapping(MappingsStr[4], new MouseAxisTrigger(MouseInput.AXIS_WHEEL, true));
        inputManager.addMapping(MappingsStr[5], new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false));
        inputManager.addMapping(MappingsStr[6], new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping(MappingsStr[7], new KeyTrigger(KeyInput.KEY_C));
        inputManager.addMapping(MappingsStr[8], new MouseButtonTrigger(0));
        inputManager.addMapping(MappingsStr[9], new KeyTrigger(KeyInput.KEY_K));
        
        ActionListener actionListener = new ActionListener()
        {
            public void onAction(String name, boolean isPressed, float tpf) 
            {
                if(name.equals(MappingsStr[Mappings.KeyUp.ordinal()]))
                {
                    inputStates[Mappings.KeyUp.ordinal()] = isPressed;
                }

                if(name.equals(MappingsStr[Mappings.KeyDown.ordinal()]))
                {
                    inputStates[Mappings.KeyDown.ordinal()] = isPressed;
                }

                if(name.equals(MappingsStr[Mappings.KeyLeft.ordinal()]))
                {
                    inputStates[Mappings.KeyLeft.ordinal()] = isPressed;
                }

                if(name.equals(MappingsStr[Mappings.KeyRight.ordinal()]))
                {
                    inputStates[Mappings.KeyRight.ordinal()] = isPressed;
                }

                if(name.equals(MappingsStr[Mappings.KeyS.ordinal()]))
                {
                    inputStates[Mappings.KeyS.ordinal()] = isPressed;
                }

                if(name.equals(MappingsStr[Mappings.KeyC.ordinal()]))
                {
                    inputStates[Mappings.KeyC.ordinal()] = isPressed;
                }

                if(name.equals(MappingsStr[Mappings.KeyK.ordinal()]))
                {
                    inputStates[Mappings.KeyK.ordinal()] = isPressed;
                }
                
                if(name.equals(MappingsStr[Mappings.MouseLeft.ordinal()]) && !isPressed)
                {
                    inputStates[Mappings.MouseLeft.ordinal()] = true;
                    System.out.println("LMB" + " " + name + " " + tpf);
                }
                
            }
        };
        
        AnalogListener analogListener = new AnalogListener()
        {
            public void onAnalog(String name, float value, float tpf) 
            {
                if(name.equals(MappingsStr[Mappings.MouseWheelUp.ordinal()]))
                {
                    inputStates[Mappings.MouseWheelUp.ordinal()] = true;
                }

                if(name.equals(MappingsStr[Mappings.MouseWheelDown.ordinal()]))
                {
                    inputStates[Mappings.MouseWheelDown.ordinal()] = true;
                }                
            }
        };
                
        inputManager.addListener(actionListener, MappingsStr);
        inputManager.addListener(analogListener, MappingsStr);
    }
    
    
    
    public boolean getInputState(Mappings id)
    {
        return inputStates[id.ordinal()];
    }
    
    public void setInputState(Mappings id, boolean value)
    {
        inputStates[id.ordinal()] = value;
    }  
    
    public Vector2f getMousePos()
    {
        return inputManager.getCursorPosition();
    }
}

I mean, mouse buttons work else everyone would be complaining so there must be some assumption in your code that is incorrect.

Step one to debugging is verifying assumptions. So put some printlns in your methods to see what they are (or aren’t) being called with.

Like, put a System.out.println() right at the top of:
public void onAction(String name, boolean isPressed, float tpf)

…instead of down inside the if block that has already assumed a half-dozen things.

I added a println at the top of the onAction method and it works for all inputs except the mouse buttons. The same strange things happens that when I press 2 mouse buttons together and then release the left one last it registers fine.

Edt: Seems like it needed 2 mouse button inputs at the same time to register

Only for you. Else all other JME games would be horribly broken, yes?

Have you tried the JME tests/examples that use mouse buttons? There are a bunch of them.

Thanks for the advice of trying the test package. I understand that the problem is in my project. At one moment I assumed that maybe my project is not set up correctly. I asked the question to see if maybe someone had encountered the same problem.

    inputManager.addMapping("Click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addMapping("RightClick", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));

    inputManager.addListener(this, "RightClick");
    inputManager.addListener(this, "Click");


    @Override
    public void onAction(String binding, boolean isPressed, float tpf) {
        switch (binding) {

            case "Click":
                System.out.println("Click");
                break;

            case "RightClick":
                System.out.println("Right Click");
                break;

            default:
                break;

        }

    }

Try something like that.

Now this is really strange. I checked out the test package examples and it works fine. I added the following code to my simpleAppInit method in Main.java:

inputManager.addMapping(“LMB”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

    ActionListener actionListener = new ActionListener()
    {

        public void onAction(String name, boolean isPressed, float tpf) 
        {
            if(name.equals("LMB"))
            {
                System.out.println("LLLLMMMMBBBB");
            }
        }
        
    };
    
    inputManager.addListener(actionListener, new String[]{"LMB"});

It still doesn’t register mouse clicks unless it recieves 2 mouse button inputs at the same time. Maybe I should reinstal jme? Or maybe there is something in the project settings I am not aware of?

Does it work if you replace that with:
inputManager.addListener(actionListener, “LMB”);

I mean, now you have an example that works and an example that doesn’t. So just make the broken one look more and more like the working on until things work.

Can it have something to do with there being additional appstates in my project???
And no it doesnt work either if i put “LMB”.

Nope. I have like 40+ app states in my projects and I can still get mouse buttons.

There is some difference in your app from the example. You just need to figure out what it is.

One way would be to cut and paste the example into your project and run that. See if it still works. Then change it bit by bit to be more like yours until it breaks.

Lol, turned out was using tonegod gui and I had a transparent element the size of the screen which took care of the first mouse button input :confused: so only the second was recorded(thats why pressing two buttons worked). Anyway thanks for all the help