# [C/SDL] input question

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

## Recommended Posts

hello, i'm having troubles with getting the input from keyboard and then using that data for my game. the game should allow the user to move the tank forward AND rotate it at the same time, but i can do or only the rotating bit or only the forward moving. i used the keystate function and the event thing and i got the same problem, i'm prolly doing something wrong. could someone show me how i can rotate and move at the same time, or some literature would be nice. ty.

##### Share on other sites
Maybe you could show us your input code?

##### Share on other sites
keystate = SDL_GetKeyState(NULL);if (keystate[SDLK_LEFT])left=1;else if (keystate[SDLK_RIGHT])right=1;else if (keystate[SDLK_UP])up=1;else if (keystate[SDLK_DOWN])down=1;//and then i'd doif(up && left){stuff}

this generally worked.. but the performance was more of a dirty hack than a real solution.

##### Share on other sites
So is it working? In your first post, you say "i used the keystate function and the event thing and i got the same problem", but in your second "this generally worked".

However, looking at the code, all your tests are excluding one another:

if (keystate[SDLK_LEFT])left=1;else if (keystate[SDLK_RIGHT])right=1;// why should there be an else here?else if (keystate[SDLK_UP])up=1;else if (keystate[SDLK_DOWN])down=1;

Did you mean:

if (keystate[SDLK_LEFT]){    left=1;}else if (keystate[SDLK_RIGHT]){   right=1;}if (keystate[SDLK_UP]){   up=1;}else if (keystate[SDLK_DOWN]){   down=1;}

##### Share on other sites
Or perhaps use a switch?

switch(event.key.keysym.sym){case SDLK_UP: break;case SDLK_DOWN:break;case SDLK_LEFT:break;case SDLK_RIGHT:break;}

//edit: read over the part about pressing 2 buttons at the same time...

##### Share on other sites
the code i posted above is in a function, and that function is called in the main loop.
i used the elses for faster computing.. hmm i'll use switch..

anyways.. this doesn't even matter, because i'm asking if there is any other way to solve this or not?