How2 trigger an Action when no Key is pressed

Hello Guys,

Ive got a normal Animation, and if the user presses (for example) ‘d’ another Animation shows up.

So when the user releases ‘d’, the standard Animation should return.

So I want to know from you, how I can listen for not pressing a key.
I already tried to trigger an Action with
inside the onAction Method of the ActionListener, because there I can acess the keyPressed Bool, but, of course, the onAction Method only gets called if the user presses a button that leads to it. So what can I do?

Is there an easy way to acess keyPressed?

Thanx for reeding + halping,


Yea, I searched in the Docs

1 Like

I solved the same issue with animantion layering. I had a class which handled that layering (just names of the animation). Means the normal animation was on layer 0 (lowest prio) and if somebody did for example press space to beat some one I layered the beat’em animation on layer 1. The class handling the layers now spit out beat’em and as soon I released the key it removed the layer 1 action and the action an layer 0 took place again.

You might even handle that the full cycle is played of an animation until you pop it. Yeah like stack like thing :slight_smile:

EDIT: I would not relay on key pressed or not to swtich from one animation to another but capsulate that in a easy to test class, which you can than isolated improve. Doing it on key stroke directly will end in something messy pretty soon IMHO (at least in my case).

Hope this helps.


Well, his real issue is that he doesn’t know how to capture the key up event. That will cause problems for him no matter what.

I don’t remember this ever being an issue. I haven’t used JME’s standard triggers in a long time, though.

1 Like

I don’t know but he IMO he tries to use the released key as a state and if you never pressed that key you will not get a ket released event. I understood it that way. That’s why I would organize the animations in a class. Default is idle, as soon you press wasd it is walking. jumping, shooting is then just something with a higher prio or maybe even merged somehow.

I did it that way so I had an object where you just could add remove animation names (just strings in my case) on key press/release.

But yeah just an idea.

1 Like

I guess you could use a mask to determine direction/state. It would accomodate diagonals and an idle state.


My recollection is that the isPressed parameter of ActionLister’s onAction() is actually set to true and false as appropriate.

If you are seeing otherwise then you will need to produce some sample code and/or a simple test case.

Else you will have everyone responding to the 100 problems that you didn’t mention and ignoring the big glaring one that you did. :wink:


@pspeed yeah, the keyPressed Bool. But how do work with it? onAction() only gets called when a key that leads to it gets pressed (I mentioned before)

I like @ia97lies idea, but that doesnt help anything at all. You said, when someone presses space animation layer 1 should appear, and when you release the key layer 1 should be removed. So, how to log, when i released the key? Did I lost something? That doesnt help :c

1 Like

No, that’s not true.

Put together a simple test case that does a System.out.println() for every call.

1 Like

Oh lol

I didnt got that you can place the onAction() Method right in the class.
Always wrapped the ActionListener arround it…
So, thats it. Thanks.

Embarassing im Sorry


Wait Wrong
Thats not true

I placed the onAction() Method unwrapped, but nothing got printed to the Console.

Have I to call it manually?
Thx for answering

1 Like

As long as the class implements it.

You can use UserData and AnimEventListener to make this happen.

Add a subclass that implements AnimEventListener where the onAnimCycleDone method looks for changes in the UserData variable of your choice. This Listener will change the animation based off that variable.

In your ActionListener use isPressed Boolean to set the UserData variable and use !isPressed to change it. As long as they keep key pressed the UserData variable will not change. Upon release it will change your UserData variable. After the animation finishes AnimEventListener will check the variable.

You can use an Enum class with getters so you can easily set your UserData variable.

What are you doing that requires the user to press a key to start an animation?

1 Like

You are adding the listener to the InputManager right?
getApplication().getInputManager().addListener(actionListener, actionInputs);

1 Like

Forgot to mention this is basically the same thing paul and ia97lies is telling you.

1 Like

Ok got that :slight_smile: I miss interpreted your question totally :sweat_smile:
What you state does not work normally just work. There are working one class examples in the wikis beginner section which do exactlly what you want…
But in the end you have to write a one class test sample to get valuable help as @pspeed already said in a previous post.