Godot mouse capturing that doesn’t feel annoying / invasive
Created: 03 Jun 2023, Modified: 03 Jun 2023
Warning: This is an unfinished article from the graveyard of unfinished articles. It has been deemed unfit to publish as an article and may contain erroneous information or be a waste of time to read. Proceed at your own peril.
instead of:
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func maybe_capture_mouse():
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
return
if get_viewport().get_mouse_position().y > 0: # i.e. not on titlebar
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _notification(what):
match what:
NOTIFICATION_APPLICATION_FOCUS_IN:
maybe_capture_mouse()
NOTIFICATION_WM_WINDOW_FOCUS_OUT:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
func _input(event):
if event is InputEventMouseButton:
maybe_capture_mouse()
- Prevents the user from being able to interact with the titlebar, can’t move the window if windowed or press titlebar buttons like minimise, maximise, or close
- Capturing mouse on launch feels invasive
with the new way player can interact with the titlebar without being interrupted. if they click in the game coordinates or alt tab back into the game then the mouse is captured, as they’d expect.
■