Results 1 to 9 of 9
- 05-22-2011, 08:04 AM #1
KeyPressed trouble with KeyListeners
I'm working on a game now where you can use the arrow keys to move a ship. Everything is fine except that the movement isn't smooth. What happens is when I first press a key (like the down key), it moves the ship 1 space in the desired direction (like down), then pauses for about a half of a second, then continues to move the ship in the direction until I release the key. What I want to happen is when I hold it down, it won't have that pause. Any Ideas?
I'm using a KeyListener with the KeyPressed() method overridden for the movement functionality.Good with: C/C++, DarkGDK, PHP, MySQL
Current reading: The Linux Programming Interface
- 05-22-2011, 09:26 AM #2
That's system dependent. The OS introduces a delay after the first key press, before starting to auto-repeat.
The way around this is to use your own javax.swing.Timer for the animation, and start the Timer on keyPressed / stop it in keyReleased. On some OSs you could also have a problem with that, inasmuch as they generate a series of key pressed/released events when a key is held down, whereas a different OS might generate only a series of keyPressed events. This may necessitate using a second Timer to introduce the desired latency.
db
- 05-22-2011, 09:29 AM #3
Sounds like the key repeat rate of your OS. I doubt there's much you can do about the timing of keyPressed calls. But I don't think keyReleased will be called until the key is actually released. If that's the case, you could begin movement on the first keyPressed, and time it smoothly until you get the corresponding keyReleased.
Get in the habit of using standard Java naming conventions!
- 05-22-2011, 01:46 PM #4
Add some print outs to the listener code with the current time to show when/if they are called.
- 05-23-2011, 05:37 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
If you begin movement on the first KeyPressed, set a boolean flag to indicate movement has started, and always check it when you get a KeyPressed event so you know whether to start movement or whether it's just a repeat-key from the OS. When you get KeyReleased, you can reset the flag.
- 05-23-2011, 06:52 PM #6
I don't think keyReleased will be called until the key is actually released.Bug ID: 5011907 KeyListener behaviour is incorrect under linuxWhen you get KeyReleased, you can reset the flag.
(Note that this was not accepted as a bug)
db
- 05-27-2011, 11:01 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Wow, I don't remembers seeing that one before - thanks for that - the bug-report comments seem reasonable - the Java API really should present these events consistently cross-platform wherever possible, and in this case I don't see that it would be particularly difficult to do - and the bug-report doesn't mention any technical problems... very odd.
- 05-28-2011, 06:24 AM #8
This was discussed in-depth on the erstwhile Sun forums, but Google can't find any of that old content nowadays -- the migration to OTN has dealt a major blow to the collective Java knowledge base. The workaround, as I mentioned earlier, is to use a Timer to introduce an artificial latency.
Problems can still arise if the key autorepeat is set to an absurdly slow rate, as this rate can't to the best of my knowledge be queried from Java (short of JNI, and I'm not familiar with that). And naturally, there will be a delay before the real keyReleased is acted on.
TBH I would consider this a Linux, not Java, bug. It would be revealing to see how native Linux games handle this.
db
- 05-28-2011, 09:31 PM #9
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
Hey this is surely the OS problem, it has default repeat initialization rate that has to be change in your case.....
Go to Contorl Panel> keyboard> speed>
here set that repeat rate slider to SHORT.
I think much of your problem will be sort out, and to be frank, aim new to java so sorry i do not know any coding changes for this.....
Similar Threads
-
Problem with KeyPressed any Help is appreciated
By gryd00 in forum New To JavaReplies: 3Last Post: 05-17-2011, 05:15 PM -
Im having trouble with.......
By Java Noobie in forum New To JavaReplies: 1Last Post: 03-12-2011, 05:06 PM -
KeyListeners not working
By superzipzop in forum AWT / SwingReplies: 3Last Post: 02-06-2011, 03:14 AM -
Problem with keylisteners
By dunafrothint in forum AWT / SwingReplies: 2Last Post: 11-23-2009, 05:54 PM -
[SOLVED] KeyListeners in JavaApplets
By Unome in forum Java AppletsReplies: 18Last Post: 10-30-2008, 10:36 PM


LinkBack URL
About LinkBacks


Bookmarks