Mouse click event

Godot Version

4.4

Question

So I see that we only have Mouse Pressed event which says whether a mouse button has been pressed or not.

But what I am after is the Mouse Clicked event which is the user presses and then releases the mouse button.

In Javascript, we have onclick for this. Referencing JavaScript DOM Events :

The onmousedown, onmouseup, and onclick events are all parts of a mouse-click. First when a mouse-button is clicked, the onmousedown event is triggered, then, when the mouse-button is released, the onmouseup event is triggered, finally, when the mouse-click is completed, the onclick event is triggered.

So if I am to do this in Godot 4, it is highly preferable that I could check for this from Input Event. Is it possible to somehow “extend” it from there and make it so I could receive “click” event directly from Input Event? Is this possible?

Help > Programming

Are you hoping to use this with buttons and UI, or with game actions?

@hexgrid i will use this with both UI and Game Actions.

With UI you’ll probably generally want to rely on the pressed signal rather than explicitly checking for mouse clicks.

You have Godot’s source, so you could directly add “mouse click” to the engine if you wanted, but it’s also relatively easy to emulate:

const CLICK_TIME:  int   = 250 # 1/4 second.
const CLICK_RANGE: float = 5.0 # pixels

var mouse_down_pos: Vector2
var mouse_down_time: int

func _input(e):
    if event is InputEventMouseButton:
        if e.button_index = MouseButton.MOUSE_BUTTON_LEFT:
            if pressed:
                mouse_down_pos  = e.position
                mouse_down_time = Time.get_ticks_msec()
            elif e.position.distance_to(mouse_down_pos) < CLICK_RANGE:
                var hold_time = Time.get_ticks_msec() - mouse_down_time
                if hold_time < CLICK_TIME:
                    _handle_mouse_click(mouse_down_pos)
                else:
                    _handle_mouse_long_click(mouse_down_pos, hold_time)
            else:
                _handle_mouse_down(mouse_down_pos)
                _handle_mouse_up(e.position)

func _handle_mouse_click(pos: Vector2) -> void:
    print("Got a click at " + str(pos))

func _handle_mouse_long_click(pos: Vector2, msec: int) -> void:
    print("Got a long (" + str(msec) + "ms) click at " + str(pos))

[...]
1 Like