How to Lock The Cursor to The Inside of a Window on Linux

How can I lock the cursor to the inside of a window on Linux?

int XGrabPointer(Display *display, Window grab_window,
Bool owner_events, unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor, Time time);

confine_to
        Specifies the window to confine the pointer in or None.

Trap cursor in Window

The regular windows message are not the best solution to drive precise controls. They inherit from the OS acceleration system, clipping and depends on other shenanigans as you can see.

The best API to receive mouse inputs is Raw Input. It has the advantage to expose the better dpi and polling rate the hardware can provide and is free of any under the hood manipulation. Once you read the mouse with this, you are free to use SetCapture and clipCursor to prevent unwanted click to an other window.

You can find the documentation here : https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx

Locking mouse pointer using xGrabPointer in Linux

This is the correct format that seems to work for me .

int g=XGrabPointer(dpy,DefaultRootWindow(dpy), true, ButtonPressMask |
ButtonReleaseMask |
PointerMotionMask |
FocusChangeMask |
EnterWindowMask |
LeaveWindowMask,GrabModeAsync,GrabModeAsync, None, None, CurrentTime);

Hiding mouse cursor with glfw

Since glfw 3.0 the API call has changed, you must use glfwSetInputMode with a pointer of your window.

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);

For more information see http://www.glfw.org/docs/3.0/moving.html

How do you hide the mouse pointer under Linux/X11?

You can create and set an invisible cursor theme. This trick is used by maemo, because it's rather pointless to have a cursor on a touchscreen device.

Sadly, the ability to change the global cursor theme at runtime is not uniform across X11 applications and toolkits. You can change the server resource Xcursor.theme, and nobody will notice (generally it's only queried at startup); you can inform xsettings which only seems to affect Gtk+ programs; KDE uses some sort of communication through properties on the root window; etc.

At least changing the cursor for your own application is as easy as XDefineCursor, and if you do that on the root window, some applications might follow along.

How to lock cursor to game window?

Quite simple in OpenTK 3.2

OpenTK.GameWindow window = new OpenTK.GameWindow();
window.CursorGrabbed = true;

You can additionally use this snippet to hide the cursor. Just implement it as needed.

window.Cursor = MouseCursor.Empty;

Locking mouse cursor in X11

I tried to compile the code on a pc with a Linux system, and it worked. Before I
was compiling it in a virtual machine, so I'm guessing that the base system has priority when it comes to the mouse.

How to set the mouse position under X11 (linux desktop)?

Sounds like you're using X, so what you probably want is XWarpPointer. To give an absolute position on the whole screen, use the Root Window as dest window.

(You can get a quick and dirty list of X functions using ls /usr/share/man/man3/ | grep '^X')

Lock mouse in window Pygame

You have to call pygame.event.set_grab(True) as well.

Better allow the users to exit with the Esc or another key, because they won't be able to click the x button anymore to close the window.

elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit = True


Related Topics



Leave a reply



Submit