• Advertisement
Sign in to follow this  

SDL_GetKeyState(NULL) Question

This topic is 4764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement