[SDL 1.2.x] Detecting key held down with SDL_GetKeyState()

Hi,

I’ve been trying to add some simple SDL input to a GLES program I’m working on. This program has to detect some key is being held down for a while, and add do something while it’s held down. Then, another different key could be held down for a while, and a different thing would be done while it’s held down.
The program has to exit when ESC is pushed or held down (I don’t mind).

The code relevant part in the I’m currently using is this one:

Code:

static void process_keyboard (){
SDL_PumpEvents();

    if(keystate[SDLK_LEFT])
    {
            fprintf (fp, "LeftArrow held down detected\n");
    }
    if(keystate[SDLK_RIGHT])
    {
            fprintf (fp, "RightArrow held down detected\n");
    }
    if(keystate[SDLK_ESCAPE]){
            exit_condition = 1;
            fprintf (fp, "ESCAPE stroke detected\n");
            SDL_Quit();
    }

}

int main (void){
Uint32 timeStep = 32;

    Uint32 timeLast;
    Uint32 timeCurrent = SDL_GetTicks();
    Uint32 timeDelta = 0;
    Uint32 timeElapsed = 0;
    Uint32 timeAccum = 0;

    keystate = SDL_GetKeyState(NULL);

    while (!exit_condition) {

            timeLast = timeCurrent;
            timeCurrent = SDL_GetTicks();
            timeDelta = timeCurrent - timeLast;
            timeAccum = timeAccum + timeDelta;

            if (timeAccum > timeStep){
                    fprintf (fp, "Processing input...\n");
                    process_keyboard();
                    timeAccum = 0;
            }

}

However, it doesn’t work as expectec. I’ve tried counless things today, no matter what I do I get the first “key held down” detected, and then, after releasing that key, no other key presses or holds are detected anymore.
Do you guys know what’s wrong here?

thanks!

However, it doesn’t work as expectec. I’ve tried counless things today, no
matter what I do I get the first “key held down” detected, and then, after
releasing that key, no other key presses or holds are detected anymore.
Do you guys know what’s wrong here?

You have to add an SDL_PumpEvents() inside your loop, this will allow SDL
to update your key state vector.–
Bye,
Gabry

However, it doesn’t work as expectec. I’ve tried counless things today,
no matter what I do I get the first “key held down” detected, and then,
after releasing that key, no other key presses or holds are detected
anymore.
Do you guys know what’s wrong here?

You have to add an SDL_PumpEvents() inside your loop, this will allow SDL
to update your key state vector.

Sorry I see there is a SDL_PumpEvents() in your code…

Maybe the problem is that you do not have SDL_Init() and you do not have a
window? Many architectures require a window to handle events.–
Bye,
Gabry

I just copied the relevant parts, sorry. I DO have an SDL_Init() call inside main:

Code:

    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
            return 0;
    }

    //Video is managed by other graphics system (EGL/GLES2), so I just call this for text console keyboard setting
    SDL_SetVideoMode( 0, 0, 16, SDL_SWSURFACE );

Which platform is this on? I don’t exactly trust SDL 1.2 to do the right
thing on Android, for example, and would prefer SDL 2.0 there instead.

Jonny DOn Tue, Feb 18, 2014 at 10:14 AM, vanfanel wrote:

I just copied the relevant parts, sorry. I DO have an SDL_Init() call
inside main:

Code:

    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
            return 0;
    }

    //Video is managed by other graphics system (EGL/GLES2), so I just

call this for text console keyboard setting
SDL_SetVideoMode( 0, 0, 16, SDL_SWSURFACE );


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi, Johnny D:

It’s on Linux X64.
I don’t mind using SDL2 instead, as the input handling stuff is very similar.
The problem is that SDL2 seems to need to create a window before starting to process keyboard input on current console, and I don’t have X11 available. It’s console EGL/GLES + SDL for input.

This same code works with no problems in X11, but not in console…
So is it’s a SDL1.2.x problem regarding fbdev backend?