Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Key Listener repeat delay

 
Levi Webb
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind, I figured out an alternative.
 
Thomas Clifford
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi, what was your solution to the delay ? A key binding ?
Thanks.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic