[gameprogrammer] Re: Key Repeat problem
- From: "David Olsen" <jolynsbass@xxxxxxxxx>
- To: <gameprogrammer@xxxxxxxxxxxxx>
- Date: Fri, 20 Jan 2006 08:32:08 -0600
Oops, for some reason, all of my recent gameprogrammer emails have been
filtered to my SDL mailing list folder, so I assumed you were using SDL.
Sorry!
-David Olsen
----- Original Message -----
From: "Stephen Smith" <gp@xxxxxxxxxxxxxxxxxxxxxxxx>
To: <gameprogrammer@xxxxxxxxxxxxx>
Sent: Friday, January 20, 2006 2:00 AM
Subject: [gameprogrammer] Re: Key Repeat problem
That's not a bad idea, but I was hoping to avoid all the movement
prediction thing. Also, I'm not really sure how it will solve the
problem, because the more the I think about it, the more I can't see a way
to writing good keyboard control in Java on Linux. Let me explain:-
Say you are writing a basic shoot'em-up. AFAIK, there are basically two
ways to control the players sprite when your interface is the KeyPressed
and KeyReleased events: you can either have the actual movement code
inside these events, such as:
public void keyPressed(KeyEvent e) {
if (e == Key_UP) {
ship.y_pos++;
}
}
The problem with this way is that the ship will move as fast as the
keyboard repeat rate, regardless of the main game loop or anything.
The other way (and the way I'm doing it in my game) is to store which keys
are currently pressed (in a boolean array), and then process them in the
game loop, such as:-
public void keyPressed(KeyEvent e) {
keys_pressed[e.getKeyCode()] == true;
}
(and set it to false on the KeyRelease as well).
public void gameLoop() {
while (true) {
if (keys_pressed[KEY_UP] == true) {
ship.y_pos++;
}
// Rest of game code here
}
}
The flaw with this, as I mentioned, is that if the player holds down a
key, then with so many keypress/release events being fired for the
duration it is held down, the key might not be marked as being pressed at
the time when the keys are being processed in the main game loop.
Can anyone tell me what I'm missing? Or should I just move to another
language? ;-) Any help will be much appreciated.
Thanks in advance,
Stephen.
Simon Mihevc wrote:
You could try saving the time of keypress event(for each pressed key)
and send the it to the server every x seconds after the keypress event.
Also did you think about doing some sort of prediction for the client
to eliminate jerky movement? With prediction I mean you do the movement
in the client immediately and fix it if the server's position is
different as client's(or similarly).
Cheers, Simon.
I think I tried that one. Doing it that way, you can only timestamp the
keypress event (as you don't want to miss a keyreleased event for
obvious reasons). However, say you set the interval for 500ms, and the
keyreleased event fires (say) 10ms after the keyreleased event, and the
user holds the key down constantly. What will happen is that the
program will only register a keypress for 10ms every 500ms that the key
is held down, leading to jerky movement. Also, unfortunately, the 10ms
isn't constant, so it's not possible to set the KeyPress interval to be
a fraction more than what this will be.
Steve.
Paul Smith wrote:
Maybe in your KeyPressed function store a timestamp in a static
variable and the next time the function is called only pass the message
on to the network server if the time elapsed is greater than a certain
number of milliseconds?
On 1/19/06, *Stephen Smith* <gp@xxxxxxxxxxxxxxxxxxxxxxxx
<mailto:gp@xxxxxxxxxxxxxxxxxxxxxxxx>> wrote:
I think this problem is purely with Java on Linux, but if anyone
can
think of a workaround it would be much appreciated.
Basically, I have two functions, KeyPressed() and KeyReleased(),
which
normally get called (only) when a key is pressed and then released
respectively. However, if you hold a key down when running it on
Linux,
it fires each one pretty much constantly in turn for as long as
the key
is held down. My program is a network client for my game, and I
send
keypress and keyrelease signals to the server. With the above
symptoms
though, the server gets clogged up with constant signals from each
client when a single key is held down.
It is a known "symptom" of Java under Linux, but can anyone think
of a
work around?
Thanks in advance,
Stephen
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
<http://gameprogrammer.com/mailinglist.html>
--
Paul Smith
Computer programmer
paul@xxxxxxxxxxxxxxxxxx <mailto:paul@xxxxxxxxxxxxxxxxxx>
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: Key Repeat problem
- From: David Olofson
- References:
- [gameprogrammer] Key Repeat problem
- From: Stephen Smith
- [gameprogrammer] Re: Key Repeat problem
- From: Paul Smith
- [gameprogrammer] Re: Key Repeat problem
- From: Stephen Smith
- [gameprogrammer] Re: Key Repeat problem
- From: Simon Mihevc
- [gameprogrammer] Re: Key Repeat problem
- From: Stephen Smith
Other related posts:
- » [gameprogrammer] Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
- » [gameprogrammer] Re: Key Repeat problem
Say you are writing a basic shoot'em-up. AFAIK, there are basically two ways to control the players sprite when your interface is the KeyPressed and KeyReleased events: you can either have the actual movement code inside these events, such as:
public void keyPressed(KeyEvent e) {
if (e == Key_UP) {
ship.y_pos++;
}
}The problem with this way is that the ship will move as fast as the keyboard repeat rate, regardless of the main game loop or anything.
The other way (and the way I'm doing it in my game) is to store which keys are currently pressed (in a boolean array), and then process them in the game loop, such as:-
public void keyPressed(KeyEvent e) {
keys_pressed[e.getKeyCode()] == true;
}(and set it to false on the KeyRelease as well).
public void gameLoop() { while (true) { if (keys_pressed[KEY_UP] == true) {
ship.y_pos++;
}// Rest of game code here } }
The flaw with this, as I mentioned, is that if the player holds down a key, then with so many keypress/release events being fired for the duration it is held down, the key might not be marked as being pressed at the time when the keys are being processed in the main game loop.
Can anyone tell me what I'm missing? Or should I just move to another language? ;-) Any help will be much appreciated.
Thanks in advance,
Stephen.
Simon Mihevc wrote:
You could try saving the time of keypress event(for each pressed key) and send the it to the server every x seconds after the keypress event.
Also did you think about doing some sort of prediction for the client to eliminate jerky movement? With prediction I mean you do the movement in the client immediately and fix it if the server's position is different as client's(or similarly).
Cheers, Simon.
I think I tried that one. Doing it that way, you can only timestamp the keypress event (as you don't want to miss a keyreleased event for obvious reasons). However, say you set the interval for 500ms, and the keyreleased event fires (say) 10ms after the keyreleased event, and the user holds the key down constantly. What will happen is that the program will only register a keypress for 10ms every 500ms that the key is held down, leading to jerky movement. Also, unfortunately, the 10ms isn't constant, so it's not possible to set the KeyPress interval to be a fraction more than what this will be.
Steve.
Paul Smith wrote:
Maybe in your KeyPressed function store a timestamp in a static variable and the next time the function is called only pass the message on to the network server if the time elapsed is greater than a certain number of milliseconds?
On 1/19/06, *Stephen Smith* <gp@xxxxxxxxxxxxxxxxxxxxxxxx <mailto:gp@xxxxxxxxxxxxxxxxxxxxxxxx>> wrote:
I think this problem is purely with Java on Linux, but if anyone can
think of a workaround it would be much appreciated.
Basically, I have two functions, KeyPressed() and KeyReleased(), which
normally get called (only) when a key is pressed and then released
respectively. However, if you hold a key down when running it on
Linux,
it fires each one pretty much constantly in turn for as long as
the key
is held down. My program is a network client for my game, and I send
keypress and keyrelease signals to the server. With the above
symptoms
though, the server gets clogged up with constant signals from each
client when a single key is held down.
It is a known "symptom" of Java under Linux, but can anyone think of a work around?
Thanks in advance,
Stephen
--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html <http://gameprogrammer.com/mailinglist.html>
-- Paul Smith Computer programmer
paul@xxxxxxxxxxxxxxxxxx <mailto:paul@xxxxxxxxxxxxxxxxxx>
--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html
--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html
--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html
--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- [gameprogrammer] Re: Key Repeat problem
- From: David Olofson
- [gameprogrammer] Key Repeat problem
- From: Stephen Smith
- [gameprogrammer] Re: Key Repeat problem
- From: Paul Smith
- [gameprogrammer] Re: Key Repeat problem
- From: Stephen Smith
- [gameprogrammer] Re: Key Repeat problem
- From: Simon Mihevc
- [gameprogrammer] Re: Key Repeat problem
- From: Stephen Smith