1

I'm a little bit new to Key Bindings, as I have been using KeyListeners for the majority up until recently when KeyListeners proved to be my biggest obstacle. I'm wondering how you would program a keyReleased-like event with KeyBindings. KeyListeners provided the easy three methods: keyPressed, keyTyped, and keyReleased, but I'm a little bit confused as for how to make this happen with Key Bindings.

Basically, when the user presses UP, I want an object to move upwards. But when the user releases UP, the object should move downwards automatically to simulate basic gravity. Here's a little bit of my code showing the UpAction class.

class UpAction extends AbstractAction
{
    public void actionPerformed(ActionEvent tf)
    {
        north = true;
        helitimer.start();
        helitimer.start();
        helitimer2.start();
        repaint();
    }
}

The three helitimers are Timer objects that start a series of Timers to increment the y position of the object continuously and smoothly. When the action upAction is invoked, the class UpAction is called and the three timers start in order to move the object.

Is there anyway I could make it so when the user releases UP, the action is no longer invoked and the timers stop?

Thanks a lot!

applemavs
  • 503
  • 3
  • 9
  • 22

1 Answers1

3

I'm wondering how you would program a keyReleased-like event with KeyBindings

Exactly the same way you do for a keyPressed event. The difference is in the KeyStroke. Read the KeyStroke API, is shows how to create a KeyStroke for a keyReleased event. There are a couple of different ways depending on how you want to create the KeyStroke.

The KeyStroke methods assume keyPressed, so you will need to add an additional parameter or additional keyword for the keyReleased event. I don't know which method you are using to create the KeyStroke so I can't tell you the exact change. Check the API for the details.

The three helitimers are Timer objects that start a series of Timers to increment the y position of the object continuously and smoothly

You should not need 3 Timer for this. One Timer should work satisfactorily.

Is there anyway I could make it so when the user releases UP, the action is no longer invoked and the timers stop?

If you can "start" a Timer in your keyPress Action, then you can "stop" the Timer in the keyReleasedAction. All you need is a reference to the Timer. Based on the code you posted you already defined the Timer as a class variable so this should not be an issue.

But when the user releases UP, the object should move downwards automatically to simulate basic gravity

Sounds to me like you will need another Timer to do this.

Another option is to always have a Timer running. Then when the UP key is pressed you make the Y increment a negative value. When the UP key is released you make the Y increment a positive value.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks, I'll refer to the API. I'm finding that 1 timer does not provide the speed I need for the object. And increasing the incrementation of the y variable just makes the program flicker. – applemavs May 19 '13 at 04:12