Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By SurfMan
  • 1 Post By DarrylBurke

Thread: KeyListener - one long then many quick

  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default KeyListener - one long then many quick

    I've noticed in my code that the KeyListener acts i little bit weird and I wonder if I can disable that.
    Lets say I have a code like this
    Java Code:
    public void keyPressed(KeyEvent e) {
     if(e.getKeyCode()==KeyEvent.VK_0){
      System.out.println("test");
     }
    }
    this will print "test" one time then wait for a bit and then print "test" like 30 times a sec.
    the question is if I can make it print "test" 30 times a sec when I first press the button and not like this
    Press -> react one time-> wait-> react multiple times

    I want
    Press -> react multiple times.

    hope I was clear enough. Thanks!

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,076
    Rep Power
    4

    Default Re: KeyListener - one long then many quick

    Isn't that just the keyboard repeat speed setting of your operating system?
    Tolls likes this.

  3. #3
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: KeyListener - one long then many quick

    Quote Originally Posted by SurfMan View Post
    Isn't that just the keyboard repeat speed setting of your operating system?
    But what if I release the program I'm making? If I can somehow can change the settings the other ones that are using the program doesn't have the same speed. can I change the speed in the code?

    btw I'm making a one press - one action listener or something like that. when I press down the key a isKeyDown boolean is turning true and the next time the code runs a boolean hasPressed is turning true. That is why I want it repeating like that.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: KeyListener - one long then many quick

    Create your own repeat.
    As a user I wouldn't want you altering my keyboard settings...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,076
    Rep Power
    4

    Default Re: KeyListener - one long then many quick

    Does it behave the same on keyDown(KeyEvent k)? You might be interested in that.

  6. #6
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: KeyListener - one long then many quick

    keyDown(KeyEvent k)? what class is that from. KeyListener doesn't contain that.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,076
    Rep Power
    4

    Default Re: KeyListener - one long then many quick

    Nevermind, I was too fast. keyDown doesn't exist indeed. I messed up the names of KeyListener.

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

    Default Re: KeyListener - one long then many quick

    For fairly consistent timing, use a Swing Timer, started in keyPressed(...) and stopped in keyReleased(...). For a more critical application, poll System.nanoTime() using a Timer and apply logic that uses the returned value, thus eliminating cumulative error.

    Note that the sequence of press and release when a key is held down is OS dependent and is not the same in Windows and Linux. You might need to use a pair of Timers to get around that.

    db

    edit You can also look into using Key Bindings. See the Oracle tutorial about the topic.
    Last edited by DarrylBurke; 03-27-2013 at 02:28 PM.
    camickr likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: KeyListener - one long then many quick

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. KeyListener
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 01-28-2013, 08:57 PM
  2. Keylistener
    By dyelax in forum New To Java
    Replies: 4
    Last Post: 09-20-2012, 08:06 PM
  3. Need help with KeyListener
    By McDucky in forum New To Java
    Replies: 3
    Last Post: 09-16-2012, 06:35 PM
  4. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 11:46 PM
  5. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 05:14 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
  •