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!

Recommended Answers

All 4 Replies

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.

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!

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.