How to detect single-click and double-click separately without using a timer?

Godot Version

4.4.1

Question

I want to detect single-click and double-click separately. But most of implement examples are ones using a timer for that purpose. Is there no way to achieve the purpose without using a timer ? Here is my code:

func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.double_click:
			print("double-click")
		elif event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
			print("single-click")

if I do single-click, it prints out single-click only. but once I do double-click, it prints out single-click and double click together in a row. Is there any idea to solve it ?

Hi!

I believe that’s the expected behaviour, maybe I misunderstood your question, but what you’re describing seems to make sense.
If you want to only print double-click when doing a double click, I believe you have to indeed use a timer; Godot has no way of knowing you are going to do a double click, so it has to consider any click, and simply mark clicks as “double” if done quickly enough after another.

I tried with _gui_input on a Control node, _input on a Node2D node, and adding a listener to an Area2D’s input_event signal. All of my tests worked this way with the exact same code.

Anyway, since you already have a event.double_click boolean working fine, I’m wondering: what are you trying to do? Maybe we can figure something out based on your use case.

Thank you. You are right, I tried to print “double-click” only. I did it with CollisionShap2D on Area2D. I need to implement item slot. if I do single-click on an item on slot, it will be attached on the mouse cursor so that I can move that item to different slot. but if I do double-click on an item on slot, it will be consumed if it can be. But I am going to implement it by using a timer as you answered. Thanks for replying my question !

1 Like