Sometimes you might want to wrap the mouse cursor within the bounds of the window in Godot, similar to what Blender does when you move the mouse near the bounds of the window. 1
To do this, we need to detect if the mouse is close to the window edge by some arbitrary margin. We have to use a margin because if the window is maximized when the mouse hits the edge of the screen, the operating system will not report mouse movement that would move the mouse past the bounds of the window since that would also move the cursor off-screen. 1
Here’s how you would use DisplayServer to warp the mouse within the bounds of the window: 2
However, this code will not work under Wayland due to unresolved issues with warp_mouse in Godot Engine. 3