How to lock mouse inside window?

I am using the default fly cam, but I am able to just move outside of window when rotating camera. How to prevent this from happening? (I tried searching, but only found people asking how to show cursor, how to disable fly cam, but nothing about this issue.)

I am using jMonkeyEngine 3.1.0 and testing on Kubuntu 16.04.

1 Like

This method is suitable for you, the cursor is placed in a frame.

1 Like

Tried it and it doesn’t help. Strange thing is that mouse lock is working as long as I don’t move game window. As soon as I move the window (via window menu hotkey → more actions → move) it breaks something and mouse lock no longer works. :frowning: Is this a bug in engine?

PS: Not moving a window is not a proper solution and also in my case I have to move the window, because it by default lays on two monitors with vastly different resolution (full HD a 4k) so I don’t even see whole window.

Perhaps you need to write your camera, which will rotate only if the cursor is within the window.

No, that’s doing it now. I want mouse (cursor) locked into window’s borders so player can’t leave window by using mouse while controlling game. Now I move mouse, rotate approx. 90 degrees in-game, mouse leaves window (I see mouse cursor from KDE leaving window) and I can no longer control the game.

Do you have your window manager configured to focus windows with the mouse over them? ie: you can only type in a console window if the mouse is over it, etc.?

No. I can have mouse over Firefox and type into IDE just fine. I am using Kubuntu (so KDE) and didn’t change much if anything from defaults. As I wrote, the weird thing is that if I don’t mess with window position (e.g. “remember window position” option in KDE or manually move window) mouse lock is working (I can spin around as I want and never leave the window). :confused:

But this is a problem of focus. It would be strange for the OS to keep focus on one application.

Is my camera example with the same problem?

The game window still has focus (it registers keyboard inputs and is highlighted in bottom panel), it is not lost when mouse cursor leaves. Games usually keep mouse cursor locked in their windows to avoid accidental leaving window and losing in game (borderless modes would be useless in moulti-monitor setups).

Unfortunately yes.

What i have to do on Kubuntu is I Click the Window as soon as it comes up however I did not notice the problem when moving the window.

I guess you already use Inputmanager.setCursorEnabled false ? Because that would recenter the Cursor each Frame so it is locked

Yes, I am setting it to false - without it I can’t swap windows, use KDE window menu or use mouse outside of the game at all (which is very undesirable - crippling user’s ability to use other applications).

Yes, then you need to change that setting so that you cant use the Mouse outside. Thats the right one for Keeping the Cursor locked.

That you cannot Release the Cursor might be a bug in lwjgl in combination with Linux unfortunately, however you can work around that when releasing the Cursor when the game defocuses (or escape for menu)

try calling
in the update loop… worked for me :slight_smile:

1 Like