File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Got a small keybinding bug Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Got a small keybinding bug" Watch "Got a small keybinding bug" New topic
Author

Got a small keybinding bug

Caleb Kemper
Greenhorn

Joined: Feb 08, 2012
Posts: 27

Hello everybody, I've been working on a 2d sidescroller for awhile now and worked through some pretty difficult bumps in the road, but this one has me stumped, because it has to do with how Java keybindings work.

Instead of using keyListeners this time around, I read up on key bindings (setup through put() and InputMaps and ActionMaps). Most said they were more used for game controls. And although the process ended up being really similar, and I didn't see any advantage, I stuck with it. Only now I'm having game issues.

One of the most common inputs from the user in a sidescroller is to hold a directional key to move the character left/right, then while still holding it, jump, and continue. Using WASD for moving and SPACE for jumping, the list of keys being pressed during this time looks like this.......AAAAAAAAAAAAAAA*SPACE**SPACE RELEASED*..... The problem is that even though the user is still holding A the whole time, at the end there, after the SPACE press and release, it is no longer recognized without the user releasing A and then pressing it again. This is very impractical to have in a game. So here's the question...

I haven't done a significant amount of figuring out how keyListeners would work in situations like this, so I'm asking before I go to change anything. Would keyListeners have the same issues? Should I switch to them or try something else? Is this a common issue?

Thanks,
Caleb
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Would keyListeners have the same issues?

probably, but try it - it's only a few lines to test it

> Should I switch to them or try something else?

assuming this is not for an applet, try something like this,
keyPressed() to start a timer (in this example, for the 'a' key),
and keyReleased() to stop the timer. the timer gives you
control over how long between intervals,

Caleb Kemper
Greenhorn

Joined: Feb 08, 2012
Posts: 27

Thanks for the quick reply. I had thought of using a timer and even tried in multiple ways. What just hit me is that I was using the java.util.Timer instead of the javax.swing.Timer (I'd noticed this before, but didn't take the time to look into it because the util Timer was getting the job done).

So I went down and looked at the javadoc for it, and I have to say, this is going to open things up quite a bit. And the method you suggested would be perfect for controlling my bindings more precisely.

Thanks for the help,
Caleb
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Got a small keybinding bug