Well, me again having problems with the Key Events

Yeah, yeah, I asked exactly the same thing a few days ago and told you
to never mind. Well, it turns out I was mistaken, so please do mind… lol.

This was my message:-----------------------------------------------------------------------

SDL_Event event;

SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

while(!exit){

while ( SDL_PollEvent(&event) ){

switch(event.type){

case SDL_KEYDOWN:
//Some code

}

}

}

Well, my program is executing the code inside the SDL_KEYDOWN case
indefinitely SOMETIMES… I mean, it may take 30 key presses before it
happens to give you an example. It behaves EXACTLY like it would if I
actually holded the key down. If I press any other key, it starts
behaving correctly again, until it happens again.

I thought that the problem could be my keyboard, but I tried it with
several keyboards and it does the same. Any ideas?

It’s really driving me crazy… Can it be that it’s sometimes missing
the KEYUP event? Because I really don’t get it.

Manuel Garc?a Cabrera escribi?:> Yeah, yeah, I asked exactly the same thing a few days ago and told you

to never mind. Well, it turns out I was mistaken, so please do mind… lol.

This was my message:


SDL_Event event;

SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

while(!exit){

while ( SDL_PollEvent(&event) ){

switch(event.type){

case SDL_KEYDOWN:
//Some code

}

}

}

Well, my program is executing the code inside the SDL_KEYDOWN case
indefinitely SOMETIMES… I mean, it may take 30 key presses before it
happens to give you an example. It behaves EXACTLY like it would if I
actually holded the key down. If I press any other key, it starts
behaving correctly again, until it happens again.

I thought that the problem could be my keyboard, but I tried it with
several keyboards and it does the same. Any ideas?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

It’s really driving me crazy… Can it be that it’s sometimes missing
the KEYUP event? Because I really don’t get it.

We’re really going to need to see the code that causes the issue…my
experience has been that code that looks like this…

	case SDL_KEYDOWN:
		//Some code
	...

…tends to have a bug in the “…” or “//Some code” part.

If you can either post your actual code, or a small test case that
reproduces the problem, we can be more helpful.

–ryan.

Hey,

Post all of your event handling code so we can check it and try it too.

Jonny D> Date: Tue, 12 Jun 2007 16:52:57 -0300> From: manugarciac5 at yahoo.com.ar> To: sdl at lists.libsdl.org> Subject: Re: [SDL] Well, me again having problems with the Key Events…> > It’s really driving me crazy… Can it be that it’s sometimes missing> the KEYUP event? Because I really don’t get it.> > Manuel Garc?a Cabrera escribi?:> > Yeah, yeah, I asked exactly the same thing a few days ago and told you> > to never mind. Well, it turns out I was mistaken, so please do mind… lol.> > > > This was my message:> > > > -----------------------------------------------------------------------> > …> > > > SDL_Event event;> > > > …> > > > SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);> > > > while(!exit){> > …> > while ( SDL_PollEvent(&event) ){> > …> > switch(event.type){> > …> > case SDL_KEYDOWN:> > //Some code> > …> > }> > …> > }> > …> > }> > > > …> > > > > > > > Well, my program is executing the code inside the SDL_KEYDOWN case> > indefinitely SOMETIMES… I mean, it may take 30 key presses before it> > happens to give you an example. It behaves EXACTLY like it would if I> > actually holded the key down. If I press any other key, it starts> > behaving correctly again, until it happens again.> > > > I thought that the problem could be my keyboard, but I tried it with> > several keyboards and it does the same. Any ideas?> > ---------------------------------------------------------------------------> > _______________________________________________> > SDL mailing list> > SDL at lists.libsdl.org> > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org> > > _______________________________________________> SDL mailing list> SDL at lists.libsdl.org> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Make every IM count. Download Windows Live Messenger and join the i?m Initiative now. It?s free.??
http://im.live.com/messenger/im/home/?source=TAGWL_June07

Let me explain what I’m doing. I’m doing a VNC client. I have two
threads, one that is constantly showing images that are received from
the net, and other that is constantly looking for mouse and keyboard
events (quit events too, of course). Well, I can show you the code in
the events thread, but I don’t see how it would help. If I put a printf
inside the case SDL_KEYDOWN it prints even when I released the key…

Ryan C. Gordon escribi?:>> It’s really driving me crazy… Can it be that it’s sometimes missing

the KEYUP event? Because I really don’t get it.

We’re really going to need to see the code that causes the issue…my
experience has been that code that looks like this…

  case SDL_KEYDOWN:
  	//Some code
  ...

…tends to have a bug in the “…” or “//Some code” part.

If you can either post your actual code, or a small test case that
reproduces the problem, we can be more helpful.

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

In article <466F5025.8090600 at yahoo.com.ar>, manugarciac5 at yahoo.com.ar
says…

Let me explain what I’m doing. I’m doing a VNC client. I have two
threads, one that is constantly showing images that are received from
the net, and other that is constantly looking for mouse and keyboard
events (quit events too, of course). Well, I can show you the code in
the events thread, but I don’t see how it would help. If I put a printf
inside the case SDL_KEYDOWN it prints even when I released the key…

Your code may help. You’ve shown us only three lines of a sample switch
statement. Your problem could possibly be as simple as a missing break
statement on a case clause causing the execution to pass through into
another case clause unintentionally.

Also, what platform is this? Win32? Linux? Other?

Further, I don’t know all the ins and out of this issue, but there are
certain SDL things you simply cannot do in another thread. Certain SDL
functions must be called in and only in the main thread. I’m not sure if
there’s a list, but using some SDL functions in different threads can
cause issues as SDL is not 100% thread safe.

Doug.

Are you saying that because you’re afraid of showing some of your code?
Without it won’t be possible to help you.

2007/6/12, Doug :>

In article <466F5025.8090600 at yahoo.com.ar>, manugarciac5 at yahoo.com.ar
says…

Let me explain what I’m doing. I’m doing a VNC client. I have two
threads, one that is constantly showing images that are received from
the net, and other that is constantly looking for mouse and keyboard
events (quit events too, of course). Well, I can show you the code in
the events thread, but I don’t see how it would help. If I put a printf
inside the case SDL_KEYDOWN it prints even when I released the key…

Your code may help. You’ve shown us only three lines of a sample switch
statement. Your problem could possibly be as simple as a missing break
statement on a case clause causing the execution to pass through into
another case clause unintentionally.

Also, what platform is this? Win32? Linux? Other?

Further, I don’t know all the ins and out of this issue, but there are
certain SDL things you simply cannot do in another thread. Certain SDL
functions must be called in and only in the main thread. I’m not sure if
there’s a list, but using some SDL functions in different threads can
cause issues as SDL is not 100% thread safe.

Doug.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well, let’s do this… I’ll write every line that’s related to SDL. I
native language is Spanish so please don’t mind the names of the
functions and variables…

I have this prototype in my .h:

static int hiloEventos(void* param);

I’m calling it like this:

SDL_Thread* thread;
thread = SDL_CreateThread(hiloEventos, this);

Now, let’s go inside hiloEventos()…

…//Do some stuff that’s not related to the issue, and would take a lot
of time to understand, but that has nothing to do with SDL.

SDL_Event evento;

SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
SDL_EventState(SDL_JOYAXISMOTION, SDL_IGNORE);
SDL_EventState(SDL_JOYBALLMOTION, SDL_IGNORE);
SDL_EventState(SDL_JOYHATMOTION, SDL_IGNORE);
SDL_EventState(SDL_JOYBUTTONDOWN, SDL_IGNORE);
SDL_EventState(SDL_JOYBUTTONUP, SDL_IGNORE);
SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
SDL_EventState(SDL_VIDEORESIZE, SDL_IGNORE);
SDL_EventState(SDL_VIDEOEXPOSE, SDL_IGNORE);
SDL_EventState(SDL_USEREVENT, SDL_IGNORE);

…//More stuff that doesn’t matter. And then the important part…

SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

while(!player->exit){

	tecla = 0;

	while ( SDL_PollEvent(&evento) || (enviar) ){

		if (player->mouse){
			SDL_GetMouseState(&x,&y);
			x += ( player->rect.x + player->verticeX);
			y += ( player->rect.y + player->verticeY);
		}

		if (player->teclado) mod = SDL_GetModState();

		switch(evento.type){
		case SDL_MOUSEBUTTONDOWN:
			switch(evento.button.button){
			case SDL_BUTTON_LEFT:
				click=1;
				break;
			case SDL_BUTTON_MIDDLE:
				click=3;
				break;
			case SDL_BUTTON_RIGHT:
				click=5;
				break;
			case SDL_BUTTON_WHEELDOWN:
				click=7;
				break;
			default:
				click=0;
				break;
			}
			break;
		case SDL_MOUSEBUTTONUP:
			switch(evento.button.button){
			case SDL_BUTTON_LEFT:
				click=2;
				break;
			case SDL_BUTTON_MIDDLE:
				click=4;
				break;
			case SDL_BUTTON_RIGHT:
				click=6;
				break;
			case SDL_BUTTON_WHEELUP:
				click=8;
				break;
			default:
				click=0;
				break;
			}
			break;
		case SDL_KEYDOWN:
			if ( (evento.key.keysym.mod & KMOD_CTRL) && (evento.key.keysym.mod &

KMOD_ALT) && (evento.key.keysym.sym == SDLK_F12) )
player->exit = true;
else if ( (player->teclado) && ( (evento.key.keysym.sym <
SDLK_NUMLOCK) || (evento.key.keysym.sym > SDLK_COMPOSE) ||
(evento.key.keysym.sym == SDLK_LSUPER) || (evento.key.keysym.sym ==
SDLK_RSUPER) || (evento.key.keysym.sym == SDLK_SCROLLOCK) ) )
tecla = player->mappearTecla(evento.key.keysym.sym);
break;
case SDL_QUIT:
player->exit = true;
break;
default:
click=0;
tecla=0;
break;
}

…//More irrelevant stuff. Then I close the while and return.

The line that’s executing many times, is:

tecla = player->mappearTecla(evento.key.keysym.sym);

It’s doing it as fast as it’s set by the enable key repeat.

Doug escribi?:> In article <466F5025.8090600 at yahoo.com.ar>, @Manuel_Garcia_Cabrer

says…

Let me explain what I’m doing. I’m doing a VNC client. I have two
threads, one that is constantly showing images that are received from
the net, and other that is constantly looking for mouse and keyboard
events (quit events too, of course). Well, I can show you the code in
the events thread, but I don’t see how it would help. If I put a printf
inside the case SDL_KEYDOWN it prints even when I released the key…

Your code may help. You’ve shown us only three lines of a sample switch
statement. Your problem could possibly be as simple as a missing break
statement on a case clause causing the execution to pass through into
another case clause unintentionally.

Also, what platform is this? Win32? Linux? Other?

Further, I don’t know all the ins and out of this issue, but there are
certain SDL things you simply cannot do in another thread. Certain SDL
functions must be called in and only in the main thread. I’m not sure if
there’s a list, but using some SDL functions in different threads can
cause issues as SDL is not 100% thread safe.

Doug.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

This is just an idea, as I’m not sure if this is your problem or not.

Read this: http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fPumpEvents

Notice two things:

  1. SDL_PollEvent() calls SDL_PumpEvents() internally. So if you’re calling
    SDL_PollEvent() then SDL_PumpEvents() is being invoked implicitly.

  2. SDL_PumpEvents() note: You can only call this function in the thread that
    set the video mode.

So, I’m wondering if the thread you call SDL_PollEvent() is the same thread
you used to set the video mode. If not, this may or may not be a problem for
you. I’ve called some SDL functions in threads and I got away with it - maybe
because I used a thread safe SDL function or I was just lucky.

Unfortunately, I have not found a “master list” of which SDL functions are
thread-safe and which are not. Some are documented as such, but I’m not
convinced that all other SDL functions are thread safe.

The only other thing I noticed is this line:

while ( SDL_PollEvent(&evento) || (enviar) ){

This means you can enter your event handler if envair is true but
SDL_PollEvent() returned false. This means the code in the while loop may
possibly be accessing evento members that are not necessarily initialized to
meaningful values. Just a thought.

Doug.

No, I’m not calling PollEvents in the same thread I call SetVideoMode…
Can that be the problem? And the enviar thing, don’t worry about that…

Doug escribi?:> This is just an idea, as I’m not sure if this is your problem or not.

Read this: http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fPumpEvents

Notice two things:

  1. SDL_PollEvent() calls SDL_PumpEvents() internally. So if you’re calling
    SDL_PollEvent() then SDL_PumpEvents() is being invoked implicitly.

  2. SDL_PumpEvents() note: You can only call this function in the thread that
    set the video mode.

So, I’m wondering if the thread you call SDL_PollEvent() is the same thread
you used to set the video mode. If not, this may or may not be a problem for
you. I’ve called some SDL functions in threads and I got away with it - maybe
because I used a thread safe SDL function or I was just lucky.

Unfortunately, I have not found a “master list” of which SDL functions are
thread-safe and which are not. Some are documented as such, but I’m not
convinced that all other SDL functions are thread safe.

The only other thing I noticed is this line:

while ( SDL_PollEvent(&evento) || (enviar) ){

This means you can enter your event handler if envair is true but
SDL_PollEvent() returned false. This means the code in the while loop may
possibly be accessing evento members that are not necessarily initialized to
meaningful values. Just a thought.

Doug.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Seems to be working now… I inverted the roles, now I check events in
the “non-thread” and do the other things in the thread (constantly
process and blit images). Thanks

Manuel Garc?a Cabrera escribi?:> No, I’m not calling PollEvents in the same thread I call SetVideoMode…

Can that be the problem? And the enviar thing, don’t worry about that…

Doug escribi?:

This is just an idea, as I’m not sure if this is your problem or not.

Read this: http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fPumpEvents

Notice two things:

  1. SDL_PollEvent() calls SDL_PumpEvents() internally. So if you’re calling
    SDL_PollEvent() then SDL_PumpEvents() is being invoked implicitly.

  2. SDL_PumpEvents() note: You can only call this function in the thread that
    set the video mode.

So, I’m wondering if the thread you call SDL_PollEvent() is the same thread
you used to set the video mode. If not, this may or may not be a problem for
you. I’ve called some SDL functions in threads and I got away with it - maybe
because I used a thread safe SDL function or I was just lucky.

Unfortunately, I have not found a “master list” of which SDL functions are
thread-safe and which are not. Some are documented as such, but I’m not
convinced that all other SDL functions are thread safe.

The only other thing I noticed is this line:

while ( SDL_PollEvent(&evento) || (enviar) ){

This means you can enter your event handler if envair is true but
SDL_PollEvent() returned false. This means the code in the while loop may
possibly be accessing evento members that are not necessarily initialized to
meaningful values. Just a thought.

Doug.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well, most of the image blitting functions aren’t thread safe either
(unless you are only doing work in-memory and not blitting to a video
surface, in which case I think you should be fine). I believe SDL
doesn’t support setting the video mode in a different thread than the
main() one due to certain OS limitations.

It would depend on what you are doing whether your solution is
portable and/or stable.On 13/06/07, Manuel Garc?a Cabrera wrote:

Seems to be working now… I inverted the roles, now I check events in
the “non-thread” and do the other things in the thread (constantly
process and blit images). Thanks