Variable not turning false

I have the following code:



[java]



public class Controls{



int xValue;

boolean lmb = false;



public void controlsSetup(InputManager inputManager) {

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

inputManager.addListener(analogListener, “LMB”);

}



private AnalogListener analogListener = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

if (name.equals(“LMB”)) {

lmb = true;

//xValue = MouseInfo.getPointerInfo().getLocation().x;

} else {

lmb = false;

}

}

};



[/java]



For some reason, lmb doesn’t get false. lmb starts as false, yet when I press the Left Mouse Button it turns true. When I release the Left Mouse Button, it doesn’t go back to false. What am I doing wrong?

Why should it? The release action’s name also equals “LMB” ;). Check for the value too (e.g. if its 0)

What value should I check? From what I understand, name remains as the ‘last pressed button’.

Also the “last depressed one”, thats what I am saying. When you lift the button off the mouse the message that is generated will still be “true” in your check. It also has the name “LMB”, just the value “0” (I think thats it for depressed).

So what can I do to bypass it? How do I know if Left Mouse button is not being pressed?

[java]if (name.equals("LMB") && value != 0) {

}[/java]

[java]public void onAnalog(String name, float value, float tpf) {

if (name.equals(“LMB”) && value != 0) {

lmb = true;

//xValue = MouseInfo.getPointerInfo().getLocation().x;

System.out.println(lmb);

}



if (value == 0) {

lmb = false;

System.out.println(lmb);

}

}[/java]



I tried checking if value was 0, but it didn’t work. It seems onAnalog only gets called when a button is pressed. Is there anything else I can do?



value can’t be accessed from outside, it seems.

Okay, then either just set it to zero at the end of each loop (simpleUpdate) or use an actionListener, its should receive a boolean with the button state.

1 Like

This is really confusing me. I am using the following code:

[java]private ActionListener actionListener = new ActionListener() {

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals("LMB") && !keyPressed) {

System.out.println("true");

} else {

System.out.println("false");

}

}

};[/java]



However, there is no output whatsoever. What am I doing wrong?

you should use and ActionListener instead for this kind of things



detect the keydown, set a boolean to true, detect the keyUp and set the boolean to false



the thing is analogListener is really for long inputs like mouse motion or joystic move etc, for keypress use ActionListener.



EDIT : -_- i didn’t read your last post, forget what i said

the correct code would be that

[java]

private ActionListener actionListener = new ActionListener() {

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“LMB”) && keyPressed) {

System.out.println(“true”);

} else {

System.out.println(“false”);

}

}

};

[/java]

Got it to work. Thanks a lot both of you.

I think you want to use ActionListener to detect the mouse down and mouse up events and AnalogListener to detect movements of the mouse (ie. changes in mouse cursor position). This works very well.



ActionListener returns a boolean for whether the mouse button is pressed or released in the event.



public void onAction(String name, boolean isPressed, float tpf)



onAction is called with isPressed=true when the button is pressed down and a seperate onAction is called with isPressed=false when the mouse button is released.

Yeah. I will use them both.

I noticed that onAnalog’s value decreases while you are reducing pressure on the button to release it. Does it show the pressure on the button?

I guess yeah. It’s really analog, so if the drivers of the device support analog input on button press JME accounts for them.

1 Like

Wow. Awesome.