Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Windows) Fix mouse focus being set to null when a captured mouse cursor leaves the window #4832

Merged
merged 1 commit into from
Oct 12, 2021

Conversation

RT2Code
Copy link
Contributor

@RT2Code RT2Code commented Oct 12, 2021

On the Windows platform, when a mouse cursor captured with SDL_CaptureMouse leaves the window client area, the SDL fire a SDL_WINDOWEVENT_LEAVE event followed immediately by a SDL_WINDOWEVENT_ENTER event on every cursor movement. As a side effect, the reported relative motion is always 0.

// The captured mouse cursor leaves the window
SDL_WINDOWEVENT_LEAVE
SDL_WINDOWEVENT_ENTER
SDL_MOUSEMOTION X=-1 Y=149 XRel=0 YRel=0

SDL_WINDOWEVENT_LEAVE
SDL_WINDOWEVENT_ENTER
SDL_MOUSEMOTION X=-3 Y=150 XRel=0 YRel=0

SDL_WINDOWEVENT_LEAVE
SDL_WINDOWEVENT_ENTER
SDL_MOUSEMOTION X=-4 Y=151 XRel=0 YRel=0

SDL_WINDOWEVENT_LEAVE
SDL_WINDOWEVENT_ENTER
SDL_MOUSEMOTION X=-5 Y=152 XRel=0 YRel=0

SDL_WINDOWEVENT_LEAVE
SDL_WINDOWEVENT_ENTER
SDL_MOUSEMOTION X=-6 Y=153 XRel=0 YRel=0

SDL_WINDOWEVENT_LEAVE
SDL_WINDOWEVENT_ENTER
SDL_MOUSEMOTION X=-7 Y=154 XRel=0 YRel=0

// SDL_CaptureMouse is disabled
SDL_WINDOWEVENT_LEAVE

The fix is to not set the mouse focus to null if the mouse cursor is captured when it leaves the window client area.

Fix #2385 and may help with #4789

The minimal code to test it :

#include "SDL.h"

int main(int, char**)
{
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window* window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_RESIZABLE);

	bool run = true;
	while (run)
	{
		SDL_Event event;
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_MOUSEBUTTONDOWN:
				if (event.button.button & SDL_BUTTON_LEFT)
					SDL_CaptureMouse(SDL_TRUE);
				break;
			case SDL_MOUSEBUTTONUP:
				if (event.button.button & SDL_BUTTON_LEFT)
					SDL_CaptureMouse(SDL_FALSE);
				break;
			case SDL_MOUSEMOTION:
				SDL_Log("SDL_MOUSEMOTION X=%d Y=%d XRel=%d YRel=%d", event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
				break;
			case SDL_WINDOWEVENT:
				if (event.window.event == SDL_WINDOWEVENT_ENTER)
					SDL_Log("SDL_WINDOWEVENT_ENTER");
				if (event.window.event == SDL_WINDOWEVENT_LEAVE)
					SDL_Log("SDL_WINDOWEVENT_LEAVE");
				break;
			case SDL_QUIT:
				run = false;
				break;
			}
		}
	}

	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SDL_CaptureMouse stops reporting relative movements at desktop edge
2 participants