Ceiling Gecko
Ceiling Gecko

Reputation: 3186

How to constrain the mouse cursor within the bounds of the window in LibGDX?

This is a widely known issue that can even sometimes appear on even high-budget professional titles, that when you set the window type to "borderless" and you have an extended display your mouse can "slide off-screen" if the mouse is not constrained to the limits of the display the game is running on currently.

I was wondering how LibGDX can tackle this.

Is there some direct way to constraint mouse movement?

Or does one need to do continuous iterative calculations on mouse position or something?

Upvotes: 3

Views: 2875

Answers (2)

Juraj Pancik
Juraj Pancik

Reputation: 301

Libgdx offers built-in function for trapping mouse cursor inside window. Function you are looking is Gdx.input.setCursorCatched(true).

Taken from the docs:

void setCursorCatched(boolean catched) Only viable on the desktop. Will confine the mouse cursor location to the window and hide the mouse cursor. X and y coordinates are still reported as if the mouse was not catched. Parameters: catched - whether to catch or not to catch the mouse cursor

Or checkout documentation by yourself here.

Upvotes: 3

Kevin Workman
Kevin Workman

Reputation: 42174

LibGDX is built on top of LWJGL, so you should be able to use the Mouse.setGrabbed() method.

More info in the API: http://www.lwjgl.org/javadoc/org/lwjgl/input/Mouse.html#setGrabbed(boolean)

And here's a discussion on what changes when you call Mouse.setGrabbed(): http://lwjgl.org/forum/index.php?topic=5150.0

Upvotes: 1

Related Questions