Prevent cursor from leaving the game's window

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dogman
:warning: Old Version Published before Godot 3 was released.

I’ve read someone placing it back into the window whenever it leaves the border with the Input.warp_mouse() method.

But is there a proper way to lock the cursor inside the window?

:bust_in_silhouette: Reply From: avencherus

I haven’t tested it, but according to the Input class, you can use:

Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

However, it also makes the cursor invisible, so you may need to render your own cursor at the mouse coordinates given.

Yeah that worked, Thanks.
Plus seems I’ll have to prevent it going off-screen as well

dogman | 2017-02-26 11:42

This doesn’t seem to work if you export to html. In that case the mouse can still leave the window.

rockstardev | 2022-07-22 15:18