-
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.
-
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
-
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
-
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
-
Ok, thanks a lot. I think I understand how it works, but could i get some sample code to show how that's done?
-
Hints: every event has a method: getWhen(). Store the result in a class variable every time you choose to process the event.
eschew obfuscation
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks