SDL_GetKeyState(NULL) Question

Started by
2 comments, last by Genjix 19 years ago
Hi, I want to use SDL_GetKeyState for Input. Here the example code from the sdl homepage:

iKeyState = SDL_GetKeyState(NULL);
if (iKeyState[SDLK_F1]) {
	// do lkhhk
So, first I get the pointer to a key matrix and then I check whether a certain key was pressed. This examples works fine. But is there a possibility to convert the SDL_GetKeyState directly to a virtual SDLKey, something like that:

iKeyState = SDL_GetKeyState(NULL);
vKey = someFunctionIDontKnow(iKeyState);
if(vKey == SDLK_F1) {
	// do lkhkhk
}
Thanks for your help!
Advertisement
The only way you could do that is if you handled the input detection in the event loop. For example, on Pnp Bios's web page he has this:
while(isRunning)    {        SDL_PollEvent(&event);        if(event.type == SDL_QUIT)            isRunning = false;        if(event.type == SDL_KEYDOWN)        {            if(event.key.keysym.sym == SDLK_ESCAPE)                isRunning = false;        }    }


You could get the key instantly by using vKey = event.key.keysym.sym, but then you would be limited to one key. If you were just testing for one key, as in that example, then that's what you would want to do. Is this what you are looking for? I mean you could always make a seperate input function and pass it the event.key.keysym.sym value so your loop is not cluttered as well.
why do you want to do this?

well, IIRC, that array is just an array of bools saying weather or not a key is pressed. so, couldnt you just do something like this:

iKeyState = SDL_GetKeyState(NULL);for(int i = 0; i < 256; i++){   if(iKeyState)   {       vKey == i;   }}if(vKey == SDLK_F1) {	// do lkhkhk


hmm, not sure how it would work since it would keep looping and reset what vKey was untill it ran through the whole array. could put a break, but then it would bail out early. if you really wanted to do this, you could put it all into a lookup table, but this is all just pointless since you already have a lookup table made.

i think im just misunderstanding maybe...

[Edited by - graveyard filla on March 31, 2005 6:03:44 PM]
FTA, my 2D futuristic action MMORPG
Quote:Original post by Drew_Benton
The only way you could do that is if you handled the input detection in the event loop. For example, on Pnp Bios's web page he has this:
*** Source Snippet Removed ***

You could get the key instantly by using vKey = event.key.keysym.sym, but then you would be limited to one key. If you were just testing for one key, as in that example, then that's what you would want to do. Is this what you are looking for? I mean you could always make a seperate input function and pass it the event.key.keysym.sym value so your loop is not cluttered as well.


this is the standard way to poll events in SDL (PollEvent() should be done before GetKeyState(NULL); otherwise the lookup table will be empty).

You can also split that loop up into a function to take the key and affect the scene.

This topic is closed to new replies.

Advertisement