Keyboard and mouse - event handling problem

Hi all!

I found the problem when used the recommended method of handling window events for mouse and keyboard on the 3D scene (mouse for the look direction, keyboard - the moving):

if(SDL_PollEvent(&ev))
{
	switch(ev.type){
		case SDL_KEYDOWN: {
			render.push_keys(&ev);
			break;
		}
		case SDL_MOUSEMOTION: {
			SDL_WarpMouseInWindow(id, iW/2, iH/2);
			render.push_mouse(&ev);
			break;
		}
	}
}

Description of the problem: on the 3D scene mouse events subpressing the events queue from the keboard. When the mouse don’t moving keyboard handling is working correctly. Otherwise, all the keboard events accumulated in the buffer and start handling only when the mouse was stop. I was found this on the Linux+Xfce4 with SDL2. On the MS-Win 8 it is insensibly.

Has anyone seen this problem? How differently to implement event handling?

I’m sorry, I want to clarify the fragment of my code:

if(SDL_PollEvent(&ev))
{
   switch(ev.type){
      case SDL_KEYDOWN: {
         render.push_keys(&ev);
         break;
      }
      case SDL_MOUSEMOTION: {
         SDL_WarpMouseInWindow(id, iW/2, iH/2);
         render.push_mouse(&ev);
         break;
      }
      default:
         render.push_empty();
   }
}

Change your **if(SDL_PollEvent(&ev)) ** into **while(SDL_PollEvent(&ev)) ** and check if it works after that. If not, let me know and I will try to help you further.

With this code the problem persists:

bool loop = true;
while(loop)
{
   while(SDL_PollEvent(&ev))
   {
      switch(ev.type){
         case SDL_KEYDOWN: {
            render.push_keys(&ev);
            break;
         }
         case SDL_MOUSEMOTION: {
            SDL_WarpMouseInWindow(id, iW/2, iH/2);
            render.push_mouse(&ev);
            break;
         }
         case SDL_QUIT: {
            loop = false;
            break;
         }
         default:
            render.push_empty();
      }
      SDL_GL_SwapWindow(id);
   }
   render.push_empty();
}
DestroyWindow();

To solve the problem I deside to use SDL_PumpEvents() function:

while (loop)
{
	SDL_PumpEvents();
	actions.btns = SDL_GetMouseState(&actions.x, &actions.y);
	actions.keys = SDL_GetKeyboardState(NULL);
	loop = check_window_events(); // catch resizing and quit events
	render.push(actions);
	SDL_GL_SwapWindow(id);	
}
DestroyWindow();

But I doubt in choosing the method of the checking window events (resize, quit) in this case. It is better to choose function for the checking window events? Can you help me to select?

Sorry it didn’t help.

To be able to help you further, I’m afraid I’ll need to see the rest of your code, especially the one that’s handling the keyboard and mouse inputs (i.e the function that handles all the ‘actions’). Do you have your code in github, dropbox or similar?

…To be able to help you further…

Thank you for your time. For myself, I already solved the issue.

Do you have a relationship to SDL2 developers? If it is necessary, I can prepare a small sample application and place the code in an open place to understand and fix the issue in the topic.

No, no relation with the SDL2 developers. Out of curiosity, was the issue caused by something in your own code or in the SDL2 library code?

was the issue caused by something in your own code or in the SDL2 library code?

I am sure that the reason is that since a library. Handling events from the mouse for some strange reason, have priority in the SDL.