Mouse Cursor Capture

Hi all.

I am trying to work out how to make Panda3D capture the mouse cursor. Basically I want to make it so that the mouse cannot leave the Panda3D window.

I am aware that one possible solution is just to make the application fullscreen, but this is not a viable solution for me as I have 2 desktop monitors. If I run the app fullscreen on the left monitor then when I move the mouse to the right edge it leaves the window.

Does anyone have any experience with how to do this?

Thanks in advance.

run this in a task and it’ll reset the cursor to the center of your window. once the mouse left the window (due to very fast movement) then there is little you can do about it. but this works fine in most situations

centerx =  base.win.getProperties().getXSize()/2
centery =  base.win.getProperties().getYSize()/2
base.win.movePointer(0,centerx,centery)

You should set the mouse mode to MRelative, on most platform that does what you want.

Hi again. I tried the following:

this->window = this->framework.open_window();
WindowProperties wp = this->window->get_graphics_window()->get_properties();
wp.set_mouse_mode(WindowProperties::M_relative);	this->window->get_graphics_window()->set_properties_now(wp);

However this had absolutely no effect on the mouse cursor. Am I doing something wrong?

Also, for ThomasEgi’s suggestion, if I do this will it mess up the GUI input? Because the mouse cursor will always be in the middle of the window?

Thanks again.

The correct way to reset the window properties is:

this->window->get_graphics_window()->request_properties(wp);

you shouldn’t be calling set_properties_now() directly. However, you also shouldn’t expect this to have an obvious effect on Windows; it is only necessary for other systems such as Mac and Linux, and only in conjunction with Thomas’ advice. (The purpose of setting relative mode is to allow you to call base.win.movePointer() successfully.)

But yes, doing this will constrain the mouse to the center of the window. If you want to instead allow the mouse to move freely within the window, but forbid it from leaving the window boundaries, there is little you can do to achieve that.

David

Okay, thanks for your help everyone. I believe I can now see a way of hacking it to make it work by switching mouse input from absolute to relative when in game and then back again when using the main menu.

Are there any plans to enable mouse capture support in Panda3D in the future or is it seen as not necessary?

“mouse capture” such as you describe isn’t a good idea, interface-wise. An application shouldn’t try to prevent the mouse from leaving the boundaries of the window; that will just frustrate the user. In fact, Mac OSX expressly disallows this kind of behavior by applications, for just this reason.

There is another sense in which the phrase “mouse capture” is used; it means that the window retains focus even though the mouse has wandered beyond the window borders. Applications typically enable “mouse capture” in this sense whenever the user clicks down on the mouse within the application window, and for as long as the user continues to hold the mouse button. Panda also behaves in this way automatically.

David

Actually, for the applications often developed with Panda3d - games - it is an extremely good idea (try playing Starcraft 2 in windowed mode without cursor capture). Even though this is an old topic, it is important to me, too, for one convenience case and one very important reason:

#1 How can you, say scroll, by moving your mouse to the edge of the window in windowed mode
and more importantly:
#2 In full-screen mode with 2 monitors the mouse will move to the second monitor. Use a click to interact with the game, and the game loses focus.

Users should use alt-tab to give focus to another program.

So, if anybody found a solution to this, a reply would be much appreciated.

I believe on Windows there is the API function

ClipCursor()

to constrain and release the cursor, but it would be very nice, if Panda could release the cursor if the window in question loses focus (by alt-tabbing for example). If every developer using Panda has to do that h(im||er)self, I can smell the errors raining all day long. On Linux I am not sure, but it should be possible as, I believe, VMWare can have that behaviour.