GeeCON Prague 2014*
The moose likes Java in General and the fly likes Key Listener repeat delay Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Key Listener repeat delay" Watch "Key Listener repeat delay" New topic
Author

Key Listener repeat delay

Levi Webb
Greenhorn

Joined: Mar 13, 2012
Posts: 10
This is my first post on this forum, and definably not my last. I intend to stick with this one for all my code related questions. I'm still starting out, and I have had no coding advice so far, so please don't judge my organization.

Now, I have been trying to make a 2D side scroller strategy game. I currently have completed a decent amount of the project - file format, reader, and the display for showing the blocks that works very nicely. I'm using the Graphics object to paint on a JPanel component on a JFrame for the game. I believe this is not nearly as efficient or capable as other ways to display graphics, but being a premature programmer, I don't know any other way.

I'm using the KeyListener for character movement. This works fine, except that there is a delay before the listener starts repeating the contained code while the key is held down. I don't mind using a different way of listening to key actions, but, again with my inferior programming skills, I do not know any other to do this. How would I solve this issue?

Here's the listener code I've written:


this code exists in the constructor method of an inner class that extends JPanel.

Any reply is appreciated.
Levi Webb
Greenhorn

Joined: Mar 13, 2012
Posts: 10
Never mind, I figured out an alternative.
Thomas Clifford
Greenhorn

Joined: Jun 29, 2012
Posts: 2
Levi, what was your solution to the delay ? A key binding ?
Thanks.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Levi, what was your solution to the delay ? A key binding ?

one of the ways to solve his question is to use keyPressed() to start a timer,
and keyReleased() to stop the timer.
Thomas Clifford
Greenhorn

Joined: Jun 29, 2012
Posts: 2
Thanks for your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Key Listener repeat delay