Auto-Repeat for PC


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Auto-Repeat for PC

  1. #1
    Join Date
    Nov 2004
    Posts
    13

    Auto-Repeat for PC

    Hi, does anyone know the code to stop the auto-repeat for a KeyPressed event?
    What I'm trying to do is use this code for a multiplayer game in which both players can press keys and have events happen at the same time. If you know an easy way to do that, let me know.

    Thanks.

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    do you mean like if you hold down the button it will do that command alot of times?

    if so, are you using the KeyListener interface? I know that the KeyListener interface has 3 methods associated with it:

    keyPressed(KeyEvent e) Invoked when a key has been pressed.

    keyReleased(KeyEvent e) Invoked when a key has been released.

    keyTyped(KeyEvent e) Invoked when a key has been typed.

    so, try using the keyPressed method, im pretty sure that it will only be activated once, but let me know if im way off
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Nov 2004
    Posts
    13
    Haha, yeah you're off by quite a bit. I already know how to do the KeyListener interfaces.

    keyPressed activates more than once if you hold it down long enough. For example, using the arrow keys to move a rectangle around the screen looks kind of like this: (objectX and objectY are the x and y upper left points of the rectangle)

    myFrame.addKeyListener(new KeyAdapter()
    {
    public void keyPressed(KeyEvent e)
    {
    if (e.getKeyCode() == e.VK_UP)
    {
    objectY -= 5;
    }
    if (e.getKeyCode() == e.VK_DOWN)
    {
    objectY += 5;
    }
    if (e.getKeyCode() == e.VK_RIGHT)
    {
    objectX += 5;
    }
    if (e.getKeyCode() == e.VK_DOWN)
    {
    objectX -= 5;
    }
    }
    }


    However, say you had another object, like a rectangle that starts somewhere else. You can't press two keys at the same time and have two KeyEvents happen. Say this were applied to Pong. One player could hold the up arrow key and prevent the other player from moving. How do i fix this?

    btw, thanks Kram for quick reply

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You prevent auto repeat by getting the time in milliseconds for the keydown/up event (or whatever the event is) . Next time it happends you check the time elapsed since last time, if to soon, then ignore, if ok you set a new time value and process the event.
    eschew obfuscation

  5. #5
    Join Date
    Nov 2004
    Posts
    13
    Ok, thanks a lot. I think I understand how it works, but could i get some sample code to show how that's done?

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Hints: every event has a method: getWhen(). Store the result in a class variable every time you choose to process the event.
    eschew obfuscation

  7. #7
    Join Date
    Nov 2004
    Posts
    13
    k, i think (hope) i can handle it from here. Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center