Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default 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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    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

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Add some print outs to the listener code with the current time to show when/if they are called.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    I don't think keyReleased will be called until the key is actually released.
    When you get KeyReleased, you can reset the flag.
    Bug ID: 5011907 KeyListener behaviour is incorrect under linux
    (Note that this was not accepted as a bug)

    db

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by DarrylBurke View Post
    Bug ID: 5011907 KeyListener behaviour is incorrect under linux
    (Note that this was not accepted as a bug)
    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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    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

  9. #9
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    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

  1. Problem with KeyPressed any Help is appreciated
    By gryd00 in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 06:15 PM
  2. Im having trouble with.......
    By Java Noobie in forum New To Java
    Replies: 1
    Last Post: 03-12-2011, 06:06 PM
  3. KeyListeners not working
    By superzipzop in forum AWT / Swing
    Replies: 3
    Last Post: 02-06-2011, 04:14 AM
  4. Problem with keylisteners
    By dunafrothint in forum AWT / Swing
    Replies: 2
    Last Post: 11-23-2009, 06:54 PM
  5. [SOLVED] KeyListeners in JavaApplets
    By Unome in forum Java Applets
    Replies: 18
    Last Post: 10-30-2008, 11:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •