Bottle Rocket
Bottle Rocket

Reputation: 367

C SDL Keyboard Events SDL_KEYUP Triggering When Key Is Down

I am using SDL in C, and I'm trying to make a character move on the screen when a key is pressed, and stop when it is released, but it seems as if the KEYUP event is triggering when the key is still being held down. If I remove the KEYUP section in pressed(), the characters will slide across the screen, but obviously won't stop on their own. If I leave the KEYUP section in, I have to press the key repeatedly to move them across the screen. Any idea what I'm doing wrong?

Here's what I have:

...

int done = 0;
while (done==0)
{
    pressed();
    if (kenter == 1)
    {
        state = 1;
        subscreen();
    }
    if (kescape == 1)
    {
        done = 1;
    }
    if (kup == 1)
    {
        playery += -2;
    }
    if (kdown == 1)
    {
        playery += 2;
    }

    if (kright == 1)
    {
        playerx += 2;
    }
    if (kleft == 1)
    {
        playerx += - 2;
    }

...

int pressed ()
{

SDL_Event keyevent;

while (SDL_PollEvent(&keyevent))
{
    switch(keyevent.type)
    {
        case SDL_KEYDOWN:
        switch(keyevent.key.keysym.sym)
        {
            case SDLK_RETURN:
            {
                kenter = 1;
                break;
            }
            case SDLK_ESCAPE:
            {
                kescape = 1;
                break;
            }
            case SDLK_a:
            {
                ka = 1;
                break;
            }
            case SDLK_s:
            {
                ks = 1;
                break;
            }
            case SDLK_UP:
            {
                kup = 1;
                break;
            }
            case SDLK_DOWN:
            {
                kdown = 1;
                break;
            }
            case SDLK_RIGHT:
            {
                kright = 1;
                break;
            }
            case SDLK_LEFT:
            {
                kleft = 1;
                break;
            }
            default:
            break;
        }
    }
    switch(keyevent.type)
    {
        case SDL_KEYUP:
        switch(keyevent.key.keysym.sym)
        {
            case SDLK_RETURN:
            {
                kenter = 0;
                break;
            }
            case SDLK_ESCAPE:
            {
                kescape = 0;
                break;
            }
            case SDLK_a:
            {
                ka = 0;
                break;
            }
            case SDLK_s:
            {
                ks = 0;
                break;
            }
            case SDLK_UP:
            {
                kup = 0;
                break;
            }
            case SDLK_DOWN:
            {
                kdown = 0;
                break;
            }
            case SDLK_RIGHT:
            {
                kright = 0;
                break;
            }
            case SDLK_LEFT:
            {
                kleft = 0;
                break;
            }
            default:
            break;
        }
    }
}
return 0;
}

Upvotes: 1

Views: 8915

Answers (3)

AcrossedKorean
AcrossedKorean

Reputation: 1

don't use switch. I experienced same thing, but I resolved into using 'if' like this. Maybe switch has 'else if'.

if(event.type == SDL_KEYDOWN){
....
}
if(event.type == SDL_KEYUP){
....
}

Upvotes: -1

user1944441
user1944441

Reputation:

I think your switch statements are broken.

Use this less confusing way

int pressed ()
{

    SDL_Event event;

    while(SDL_PollEvent(&event) )
    {
        if(event.type == SDLK_KEYDOWN)
        {
                switch(event.key.keysym.sym)
                {
                    case SDLK_RETURN:
                        doStuff = 1
                        break;
                    default:
                        break;
                }
        }
        if(event.type == SDLK_KEYUP)
        {
                switch(event.key.keysym.sym)
                {
                    case SDLK_RETURN:
                        doStuff = 0;
                        break;
                    default:
                        break;
                }
        }
    }   
}

Also important:

SDL Tutorials: Practical Keyboard Input

Oh and avoid using global variables!

Upvotes: 3

unwind
unwind

Reputation: 400009

Your use of two switch statements is odd and confusing, you should probably fix that.

The issues you're seeing are likely due to keyboard repeat, see the SDL_EnableKeyRepeat() call for how to disable it.

Upvotes: 1

Related Questions