Detecting when a key is held?

Hey everyone,

At the moment I am trying to implement acceleration/deceleration code into
my game Ickle Racers, but have hit a problem. I am trying to detect when
the up arrow key is being held down to accelerate and released to
decelerate. My code looks like this so far:

SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_UP)
{
while ( curspeed != maxspeed )
{
curspeed++;
}
}
}

if(event.type == SDL_KEYUP)
{
    // Reduce car speed when we let go of button
    if(event.key.keysym.sym == SDLK_UP)
    {
        while ( curspeed > 0 )
        {
            curspeed--;
        }
    }
}

} //end event loop

I have realised my error, I am only increasing the car speed once WHEN the
up button is pressed down, but it doesn’t go on to increase it while the
button is still held (which is what I need to do). The same is then
happening when the button is released, it is only decreasing the speed by
one - so after the button is pressed for the first time, it gets stuck in a
rutt and the speed can never go up or down.

Is there any way around this without using the SDL_Event? I basically need
to detect when the up key is pressed and held to increase the car speed, and
when the up key is released to start slowing the car gradually down to zero.

Thanks in advance.
Tane
http://tane.cream.org

you need to enable key repeat:

SDL_EnableKeyRepeat(100, SDL_DEFAULT_REPEAT_INTERVAL)

/.Line72On Saturday 08 February 2003 7:35 pm, Tane Piper wrote:

Hey everyone,

At the moment I am trying to implement acceleration/deceleration code into
my game Ickle Racers, but have hit a problem. I am trying to detect when
the up arrow key is being held down to accelerate and released to
decelerate. My code looks like this so far:

SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_UP)
{
while ( curspeed != maxspeed )
{
curspeed++;
}
}
}

if(event.type == SDL_KEYUP)
{
    // Reduce car speed when we let go of button
    if(event.key.keysym.sym == SDLK_UP)
    {
        while ( curspeed > 0 )
        {
            curspeed--;
        }
    }
}

} //end event loop

I have realised my error, I am only increasing the car speed once WHEN the
up button is pressed down, but it doesn’t go on to increase it while the
button is still held (which is what I need to do). The same is then
happening when the button is released, it is only decreasing the speed by
one - so after the button is pressed for the first time, it gets stuck in a
rutt and the speed can never go up or down.

Is there any way around this without using the SDL_Event? I basically need
to detect when the up key is pressed and held to increase the car speed,
and when the up key is released to start slowing the car gradually down to
zero.

Thanks in advance.
Tane
http://tane.cream.org


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


19:40:07 up 4 days, 8:55, 2 users, load average: 0.04, 0.11, 0.09

Is there any way around this without using the SDL_Event? I basically need
to detect when the up key is pressed and held to increase the car speed, and
when the up key is released to start slowing the car gradually down to zero.

You can use this function:

Uint8 *SDL_GetKeyState(int *numkeys);

Use it like this:

Uint8 *keystate = SDL_GetKeyState(NULL);
if(keystate[SDLK_UP]){
// Up key pressed
}

For more info see the docs.On Sun, 9 Feb 2003 01:35:23 -0000 “Tane Piper” <tane.piper at ukonline.co.uk> wrote:


Sebastian Garcia <@Seba>
Usuario Linux registrado #225450
Debian GNU/Linux ‘Sid’ kernel 2.4.20 sobre AMD K6 II
JabberID: sebag at jabber.org

Tane Piper wrote:

Hey everyone,

At the moment I am trying to implement acceleration/deceleration code into
my game Ickle Racers, but have hit a problem. I am trying to detect when
the up arrow key is being held down to accelerate and released to
decelerate. My code looks like this so far:

SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_UP)
{
while ( curspeed != maxspeed )
{
curspeed++;
}
}
}

if(event.type == SDL_KEYUP)
{
    // Reduce car speed when we let go of button
    if(event.key.keysym.sym == SDLK_UP)
    {
        while ( curspeed > 0 )
        {
            curspeed--;
        }
    }
}

} //end event loop

I have realised my error, I am only increasing the car speed once WHEN the
up button is pressed down, but it doesn’t go on to increase it while the
button is still held (which is what I need to do). The same is then
happening when the button is released, it is only decreasing the speed by
one - so after the button is pressed for the first time, it gets stuck in a
rutt and the speed can never go up or down.

Is there any way around this without using the SDL_Event? I basically need
to detect when the up key is pressed and held to increase the car speed, and
when the up key is released to start slowing the car gradually down to zero.
Try keeping a flag for when the key is up/down. I’ve made an event
wrapper for keyboard/mouse input. When the events are generated, the
class uses static variables to track which keys and buttons are current
pressed and for how long.

At 2003-02-09 02:35, you wrote:

if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_UP)
{
while ( curspeed != maxspeed )
{
curspeed++;
}

This is very strange. If the key is pressed, you run a while loop that will
increase the speed until maxspeed it hit. This is the same as a statement
’curspeed = maxspeed’ instead of the while-loop. This is most likely not
what you want. (Same applies to your decelleration code)

I have realised my error, I am only increasing the car speed once WHEN the
up button is pressed down, but it doesn’t go on to increase it while the
button is still held (which is what I need to do). The same is then
happening when the button is released, it is only decreasing the speed by
one - so after the button is pressed for the first time, it gets stuck in a
rutt and the speed can never go up or down.

Very simple: Make a variable called ‘accelleration’ and assign it. In
pseudo-code:
if (keydown)
accelleration=1;
if (keyup)
accelleration=-1;

Then in your game-loop code you can do something like:
if ((curspeed+accelleration>0) && (curspeed+accelleration<maxspeed))
curspeed+=accelleration;

Keep in mind that you should somehow time the speed/accelleration or you
will have very fast accelleration on very fast machines (or for example
when the screen refresh-rate isn’t what you expected).

Another tip: You might want to make decelleration slower than
accelleration. I believe this will make gameplay nicer by allowing the
player a little bit easier to keep a constant speed (below max). You could
do: accelleration=2 for acceleration and =-1 for decelleration for example
(or you can work with floating point values depending on the accuracy you
need).

Good luck!
Maarten.

This way you’ll never stop the vehicle(or reach the maximum speed).
It’s better to do this:

curspeed += acceleration;
if(curspeed < 0)
curspeed = 0;
if(curspeed > maxspeed)
curspeed = maxspeed;On Sun, Feb 09, 2003 at 01:38:54PM +0100, M. Egmond wrote:

At 2003-02-09 02:35, you wrote:

if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_UP)
{
while ( curspeed != maxspeed )
{
curspeed++;
}

This is very strange. If the key is pressed, you run a while loop that will
increase the speed until maxspeed it hit. This is the same as a statement
’curspeed = maxspeed’ instead of the while-loop. This is most likely not
what you want. (Same applies to your decelleration code)

I have realised my error, I am only increasing the car speed once WHEN the
up button is pressed down, but it doesn’t go on to increase it while the
button is still held (which is what I need to do). The same is then
happening when the button is released, it is only decreasing the speed by
one - so after the button is pressed for the first time, it gets stuck in a
rutt and the speed can never go up or down.

Very simple: Make a variable called ‘accelleration’ and assign it. In
pseudo-code:
if (keydown)
accelleration=1;
if (keyup)
accelleration=-1;

Then in your game-loop code you can do something like:
if ((curspeed+accelleration>0) && (curspeed+accelleration<maxspeed))
curspeed+=accelleration;


Ivan Stankovic, @Ivan_Stankovic