←⌂

Godot mouse capturing that doesn’t feel annoying / invasive

godot gamedev

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()

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.

Improve this page / Leave a message.

←⌂ / ←Functional IME in Linux Qt applications (Fcitx / IBus) / Deciphering swansontec’s C++ Map Macro→