sdl key press delay

hi

when you press a key it automatically delays a bit then starts repeating without delay.

the second key pressed action comes with a delay and i want it to start repeating without delay. and could not find anything useful.

i might not be able to tell what i want exactly. ok press simply a key and dont release it in a text editor. you will realize a delay at second character showing up
Depends on what version of SDL you are using...I don't think 2.0 supports this function but I'm sure it has something similar.

http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlenablekeyrepeat.html
Is that what you are looking for?
This is not an issue with SDL, but is how the OS reports keypresses to all applications. (The delay and fire rate are configurable in window's control panel somewhere).

The problem is that SDL keydown events are to indicate when the key is first pressed. They are not designed to tell you whether or not a key is currently being held down. For that you want to poll the realtime state of the key.

If you want the realtime state of the key, SDL offers a SDL_GetKeyState function:

1
2
Uint8 *keystate = SDL_GetKeyState(NULL);
if ( keystate[SDLK_RETURN] ) printf("Return Key currently being held.\n");



Alternatively, if you do not want to do that... you can catch the key_down and key_up events and have them set a bool to let you know the current key state.


EDIT: Ninja'd by James. I was unaware of that enable key repeat function. Though I admit I'm surprised it exists.
Last edited on
@James2250 yes it is what i m looking for but it does not exist in sdl 2.0 and i couldnt find whatever is similar

@Disch and i already use sdl_getkeystate what i need is no delay

thx both of you anyways
Last edited on
@Disch and i already use sdl_getkeystate what i need is no delay


SDL_GetKeyState has no delay. It gives you the real-time state of the key.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct keysForTest
{
    bool leftKey = false;
    bool rightKey = false;
    bool upKey = false;
    bool downKey = false;
    bool quitKey = false;
};

keysForTest myKeys;

void resetKeys()
{
    myKeys.leftKey = false;
    myKeys.rightKey = false;
    myKeys.upKey = false;
    myKeys.downKey = false;
    myKeys.quitKey = false;
}


const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
SDL_Event event;

        //---------------------------------------------------------------------
        while(SDL_PollEvent(&event))
        {
             case SDL_KEYDOWN:
                //-----------------KEYS----------------------
                if(keyboardState[SDL_SCANCODE_A])
                    myKeys.leftKey = true;
                if(keyboardState[SDL_SCANCODE_LEFT])
                    myKeys.leftKey = true;
                if(keyboardState[SDL_SCANCODE_D])
                    myKeys.rightKey = true;
                if(keyboardState[SDL_SCANCODE_RIGHT])
                    myKeys.rightKey = true;
                if(keyboardState[SDL_SCANCODE_W])
                    myKeys.upKey = true;
                if(keyboardState[SDL_SCANCODE_UP])
                    myKeys.upKey = true;
                if(keyboardState[SDL_SCANCODE_S])
                    myKeys.downKey = true;
                if(keyboardState[SDL_SCANCODE_DOWN])
                    myKeys.downKey = true;
                if(keyboardState[SDL_SCANCODE_Q])
                    myKeys.quitKey = true;
                if(keyboardState[SDL_SCANCODE_ESCAPE])
                    myKeys.quitKey = true;
                //-----------------KEYS----------------------
                break;
        }


1
2
        //after logic funcs
        resetKeys();


it might be lame coding but i m trying to get better and for now it is ok.

and to my guess it is default window settings which could be override with some function just like it was at sdl 1.2

just press a key and you will see it has a delay before starts repeating. i dont want it thats the problem. and no good game has such a delay
Last edited on
take that code out of your event loop/switch. The SDL_KEYDOWN event is only sent when the key is first pressed. It is not sent constantly.

So yes you are seeing delay here... not because SDL_GetKeyState has delay... but because your code is only running when SDL sends you a keydown event (which DOES have delay).

So yeah... you are not dealing with events here. You are just polling the key state. So take this out of your event loop.
Last edited on
thx @Disch it fixed the prob
Topic archived. No new replies allowed.