Detecting a single keypress, only on key release

Hello jme community! For my pause/unpause system, I want the pause gui to only open on release of the pause (escape) key. What happens right now is that the gui quickly opens and closes when while holding the pause key. I want the pause menu to open only on key release. (so toggling the ispaused variable on release). Is there any way to do this?
Here is my current code:

public void onAction(String name, boolean isPressed, float tpf) {
	if (name.equals("Pause")) {
		setPause(!isPause());
	}
}
1 Like

Yes you can make it only trigger the code on release by checking the isPressed boolean like this:

if (name.equals("Pause") && !isPressed) {
	setPause(!isPause());
}

This part of the hello input tutorial goes into better detail:

This is the opposite of what OP is trying to do.

OP wants to perform an operation on the transition from down to up.

Aside from that, you use a lot of code to make a pattern that should be very rare. It’s not really clear why you’d want to check the state of the key outside of a listener. For me, I’ve literally never needed that in any game I’ve ever written. So you could have some different ways to look at the way you handle keys.

1 Like

I stand corrected, @TKDKid1000 shouldn’t need my code for this .

How come it’s supposed to be rare? I’ve been using that pattern to fire my shooting code.

What do you mean be that?

But why add three classes when you could just do the shooting from the listener?

And note: if the JME listener logic puzzles you there is also Lemur’s input handling (which is superior in many ways) where you can treat a button just like any analog input, ie: “do this thing while the button is down.”

1 Like