Jump to content
  • Advertisement
Sign in to follow this  
BennettSteele

SDL Key press help- NEED BAD HELP

This topic is 2494 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

So im making my game, right? and in my void KeyPress() function, it creates a local SDL Event, then changes up,down,left and right to true/false depending on the key state. but in my game, when you press a button, it quickly presses then releases the button. Does anyone know what is happening?!?

Share this post


Link to post
Share on other sites
Advertisement

Does anyone know what is happening?!?

No, no one knows what is happening ;) But, if you could post the relevant code, I'm sure someone will be able to help out :)

Share this post


Link to post
Share on other sites
Key presses:


while (SDL_PollEvent(&keyevent))
{
if (keyevent.type == SDL_KEYDOWN)
{
switch(keyevent.key.keysym.sym)
{
case SDLK_LEFT:
left = true;
Log.Write("LEFT_PRESSED\n");
break;
case SDLK_RIGHT:
right = true;
Log.Write("RIGHT_PRESSED\n");
break;
case SDLK_UP:
up = true;
Log.Write("UP_PRESSED\n");
break;
case SDLK_DOWN:
down = true;
Log.Write("DOWN_PRESSED\n");
break;
default:
break;
}
}
if (keyevent.type == SDL_KEYUP)
{
switch(keyevent.key.keysym.sym)
{
case SDLK_LEFT:
left = false;
Log.Write("LEFT_RELEASED\n");
break;
case SDLK_RIGHT:
right = false;
Log.Write("RIGHT_RELEASED\n");
break;
case SDLK_UP:
up = false;
Log.Write("UP_RELEASED\n");
break;
case SDLK_DOWN:
down = false;
Log.Write("DOWN_RELEASED\n");
break;
default:
break;
}
}
if(keyevent.type==SDL_QUIT)
{
isRunning=false;
Log.Write("UNKOWN_QUIT_COMMAND\n");
}
if (keyevent.key.keysym.sym==SDLK_ESCAPE)
{
isRunning=false;
Log.Write("ESC_PRESSED\n");
Log.Write("Saving Map...\n");
myMap.Save("stuff/save.txt");
Log.Write("Saved!\n");
}
if (keyevent.key.keysym.sym==SDLK_q)
{
myMap.Load("stuff/save.txt");
}
if (keyevent.key.keysym.sym==SDLK_p)
{
if(Playing && Paused.Done){Game.Pause();}
else if(Playing==false && Paused.Done){Game.Resume();}
Log.Write("P_PRESSED\n");
}
}

Share this post


Link to post
Share on other sites
The code's a little hard to follow because of the formatting (you can use 'code' tags to fix that), but here's a couple of things I noticed:

1. For checking the event type, a switch statement or series of if-else's would be more appropriate (since an event can't be of multiple types).

2. It looks like you're checking for some key press events even when the event type may not be 'key down'. This may end up working anyway, but those checks should really be inside the 'key down' block.

3. Since the event can be any of several types (key press or release, 'quit', mouse, etc.), 'keyevent' is a misleading variable name; I'd just call it 'event'.

I'm not clear on what the problem behavior described in your original post is, but generally these sorts of problems can be solved be using the debugger and/or adding appropriate debug output (like the 'log' statements that you have already).

Share this post


Link to post
Share on other sites
What happens is when you were to say press up, in the first loop you would have the up key pressed. but in the next loop, it automatically says in the keypress check that up has been released even though it is still being pushed. I have no idea what is happening.

Share this post


Link to post
Share on other sites
What version of SDL are you using?

Can you post a complete, minimal example that demonstrates the behavior? For example, a single main.cpp file that does nothing more than create a window, run an event loop, and indicate somehow (e.g. console output, or perhaps setting the window title) when a specific key has been pressed and when it's been released.

(When posting the code, be sure to use 'code' tags. Also, I suggest converting all tabs to spaces before posting in order to have a better chance of preserving the formatting.)

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!