954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with SDL Key events C++

Hi, DaniWeb forum members! I am an above-amateur programmer and this is my first more complex SDL-based program.
I am trying to make the menu of a game, but SDL cannot capture the key presses when a single key was pressed. I use only UP and DOWN arrows, in this code fragment i need to switch between the choices of the menu, but when i hit only one arrow key nothing happens. Instead of that when i hit two arrows (might even be UP and LEFT, any two keys), just then SDL_PollEvent() realizes I have hit a button. I have seen it in the debug and it is strange. I am using Microsoft Visual Studio 2010, SDL library version: 1.2.14. At first it worked almost fine when I used SDL_GetKeyState(), I think this was it`s name. But with it, it switched the options like crazy and I replaced it with SDL_PollEvent(), because it is more sophisticated. The code below is even not my own, but copied from the web, it was the same at different forums. So, is it my code wrong, or it might be the fact that I am using a laptop or because of the SDL version?

In the declaration of the class:

SDL_Event Event;


In a function about events:

while( SDL_PollEvent( &Event ) )
		{
			switch( Event.type )
			{
			/* Look for a keypress */
            case SDL_KEYDOWN:
                /* Check the SDLKey values and move change the coords */
                switch( Event.key.keysym.sym )
				{
                    case SDLK_UP:
                        active--;
                        break;
                    case SDLK_DOWN:
                        active++;
                        break;
                    default:
                        break;
                }
            }
        }


Thanks for those who would help me!

Goshutu
Newbie Poster
20 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

SDL_PollEvent returns 0 when no events are left to process, perhaps this happens more often than you realize?

Also the nature of polling implies if you wait too long between polls (doing work in your switch) you may miss something.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

How didn`t I thought of that earlier? I forgot that in main() I use SDL_PollEvents().
I will try this in a few minutes, but nevertheless, thank you for telling me the obvious!

Goshutu
Newbie Poster
20 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

The dox also say it's used to add events to the queue, so after you process one in main I suppose you can put it back on the queue...
Not sure how that will pan out though.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

Okay, thanks for that, and you were right, it did pop out the event for key pressing, now i fixed it!

Goshutu
Newbie Poster
20 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: