This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes key update Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "key update" Watch "key update" New topic
Author

key update

Nikos Stavros
Ranch Hand

Joined: Feb 24, 2006
Posts: 243

I want to make a pong game. However when I moved the paddles with the key board I only get a smooth movement and the key had been down for say 0.5 seconds. Similar to the way in a word document if you hold a key, the program waits for a short time before a running sequence of letters get printed: eg

sssssssssssssssssssssssss

does anyone have any ideas.


Jesus lives
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
use a timer to move the paddle

keyPressed starts the timer (also checks if timer is running, or boolean flag keyDown)
keyReleased stops the timer (resets keyDown to false)
Nikos Stavros
Ranch Hand

Joined: Feb 24, 2006
Posts: 243

Thanks for that advice Michael

I have another question.

If I want 2 players, is it possible to avoid the keyboard conflict when 2 or more buttons are pressed, or will I have to resort to using a mouse for 2 players
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
seems to work OK for a couple of keys (in example left and right keys).
if you develop a conflict, try wrapping the actions in a SwingUtilities.invokeLater()

Nikos Stavros
Ranch Hand

Joined: Feb 24, 2006
Posts: 243

in the following code


is it possible to avoid triggering public void keyPressed in a certain condition so that the event would not be fired.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it's going to fire if a listener is aded, but the code within keyPressed()
can be conditional

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: key update
 
Similar Threads
JSpinner keyboard input event
Euler problem 8
Passing the JSON Array to the servlet
Getting primary keys