Page 1 of 1

Restrict mouse pointer to area of GLCanvas

Posted: Mon Aug 05, 2019 1:11 pm
by mikegl

I want to do the following:
1. A user presses CTRL+F10 and the mouse cursor moved to the center of GLCanvas
2. The cursor gets transparent while it gets moved inside GLCanvas
3. When the user moves the cursor outside of GLCanvas (e.g. outside the right border of GLCanvas),
I want to restrict it to the border, so the user is not able to go outside until he does step 4
4. If the user presses CTRL+F10 again the mouse cursor gets displayed again and the area
of movement is no more restricted

I have managed to do step 1,2 and 4, but I have a problem of doing step 3. This is because when
the cursor gets moved right outside of GLCanvas it doesn't get locked to the border. To determine
if the cursor is inside GLCanvas I have handled the events as follows:

Code: Select all

Bind(wxEVT_ENTER_WINDOW, &GLCanvas::OnEnterWnd, this);
Bind(wxEVT_LEAVE_WINDOW, &GLCanvas::OnLeaveWnd, this);
I have tried several things like the following, but it doesn't work.

Code: Select all

void GLCanvas::OnEnterWnd(wxMouseEvent & event)
	if (mbMousePointerHidded)
		wxPoint mouseScreenPos = wxGetMousePosition();	// Gets the mouse in screen coordinates
		int mouseX = mouseScreenPos.x - GetScreenPosition().x;	// Gets the x-mouse position relative to widget
		int mouseY = mouseScreenPos.y - GetScreenPosition().y;  // Gets the y-mouse position relative to widget
		wxPoint sceneArea = GetScreenPosition();
		int sceneAreaScreenCoordX = sceneArea.x + GetRect().width;
		int sceneAreaScreenCoordY = sceneArea.y + GetRect().height;
		// Locks the mouse to the GLCanvas scene
		if (mouseScreenPos.x > sceneAreaScreenCoordX) {
			WarpPointer(sceneAreaScreenCoordX, mouseY);
The calculation and check get done in screen coordinates. In general, I would like to omit WarpPointer because
I have read that it doesn't work on MacOS and I want to keep platform-independent.

Can someone help, please?

Re: Restrict mouse pointer to area of GLCanvas

Posted: Mon Aug 05, 2019 2:51 pm
by T-Rex
There are dedicated platform-specific functions for this: ... clipcursor ... ter.3.html (+confine_in)

You could create a wrapper class with #ifdef'ed logic for this.

Re: Restrict mouse pointer to area of GLCanvas

Posted: Mon Aug 05, 2019 3:43 pm
by mikegl
Thank you for the links and help I will take a look for this solution you provided. Maybe I will change the logic I had at first considered.

Thank you again T-Rex, the solution wrapping the win ops in a separate class was awesome this solved my problem.
I created a Windows class and in future, all the additional win ops will be implemented inside. I will do later the same
for MacOS, Linux etc. operations.