Jump to content
  • Advertisement
Sign in to follow this  
mmojz

keyboard input with SDL

This topic is 4762 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 am a beginner. problem occur while SDL programming. follow: that, I press rightarrow and uparrow at the same time, but if depress uparrow, rightarrow is not working. help me.. :-( sorry, I am used not to English. therefore, none of searching power. [Edited by - mmojz on October 10, 2005 8:21:41 AM]

Share this post


Link to post
Share on other sites
Advertisement
It's not working because of the logics you have programmed in. The easiest way to get that done is something like this (pseudo code):


Main Loop
{
SDL_PollEvent handeling
{
}
/****************************************************/
Uint8* keys = SDL_GetKeyState(0);
if( keys[SDLK_RIGHT] && keys[SDLK_UP] )
{
W/e you want to do when both are pressed
// If you want it so it's only one event per press
// keys[SDLK_UP] = 0;
// keys[SDLK_RIGHT] = 0;
}
if( keys[SDLK_RIGHT] )
{
W/e you want to do when one is pressed
// If you want it so it's only one event per press
// keys[SDLK_RIGHT] = 0;
}
if( keys[SDLK_UP] )
{
W/e you want to do when one is pressed
// If you want it so it's only one event per press
// keys[SDLK_UP] = 0;
}
/****************************************************/
Update
{
}

Draw
{
}
}




If you can post some code, make sure to use the [source] your code goes here [/source] tags, we can take a look at what you are doing right now.

Share this post


Link to post
Share on other sites
Quote:
Original post by mmojz
hi
i am a beginner.

problem occur while programming at SDL.


follow:
that, I press rightarrow and uparrow at the same time, but if depress uparrow, rightarrow is not working.

help me.. :-(

sorry, I am used not to English. therefore, none of searching power.


Could you post your code? Like the Drew_Benton said, I don't think it's a problem with SDL.

Share this post


Link to post
Share on other sites
Yeah, exactly like drew said. It's your type of input. Now, since you posted absolutely no code, and you didnt say what language you're using(sdl is availible for java, c, C++, etc.), I'm just gonna have to guess you're using C++ as it is the most widely used. Most likely, you have a main loop where everything is processed. Something like this?

while(gameIsRunning)
{
// Update my game.
}


Well, you can have something like this:

while (gameIsRunning)
{
UpdateGame();
}

// Where UpdateGame() is

void UpdateGame()
{
Uint8 *keys = SDL_GetKeyState(NULL);

if (keys[...])//some key
{
...
}

if (keys[...] && keys[...])//some two keys pressed at the same time
{
...
}
}




SDL_GetKeyState(0); gets the active keys, and returns if a key is true or false. If you want to do one key, have one key. If you want two keys at the same time, you do what I did for my SDL app, have them in an if(...) {}.

Hope it helps. Again, please post your code, and if you dont understand something, ask away.

-DBZ-

Share this post


Link to post
Share on other sites
thx

before


MainLoop
{
while(SDL_PollEvent)
{
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
case SDL_KEYDOWN:
Uint8* keystate;
keystate = SDL_GetKeyState(NULL);
if ( keystate[SDLK_UP] ) y-=6;
if ( keystate[SDLK_RIGHT] ) x+=6;
if ( keystate[SDLK_LEFT] ) x -= 6;
if ( keystate[SDLK_DOWN] ) y += 6;
if(keystate[SDLK_ESCAPE]) done = 1;
break;
}
}
Update
Render
}







after

MainLoop
{
while(SDL_PollEvent)
{
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
case SDL_KEYDOWN:
break;
}
}
Uint8* keystate = SDL_GetKeyState(NULL);
if ( keystate[SDLK_UP] ) y-=6;
if ( keystate[SDLK_RIGHT] ) x+=6;
if ( keystate[SDLK_LEFT] ) x -= 6;
if ( keystate[SDLK_DOWN] ) y += 6;
if(keystate[SDLK_ESCAPE]) done = 1;


Update
Render
}







This problem is solved, and I am touch by you~.

[Edited by - mmojz on October 9, 2005 11:32:16 PM]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!