How to handle click and doubleclick on the same node different?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mrkaio

Hello,

how can I handle click and doubleclick on the same node different?

extends Area2D

func _on_MouseClick_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.doubleclick:
		print("double")
	elif event is InputEventMouseButton and event.pressed:
		print("single")
		

Each time I do one doubleclick on the node the output in the log is:

single 
double

I had kind of same issue. I needed to know if the user did a single click or a double click.

My solution was this:

var LastPressedTime = 0

func _process(delta):
	if (LastPressedTime > 0):
		LastPressedTime = max(0, LastPressedTime - delta)
		if (LastPressedTime == 0):
			OnSingleClick(get_local_mouse_position())

func _input(event):
	if event is InputEventMouseButton and event.pressed:
		if (event.button_index == BUTTON_WHEEL_UP || event.button_index == BUTTON_WHEEL_DOWN):
			print("scrolling...")
		else:
			if (LastPressedTime > 0):
				OnDoubleClick(get_local_mouse_position())
				LastPressedTime = 0
			else:
				LastPressedTime = 0.4

func OnDoubleClick(position):
	print("double: "+str(position.x)+","+str(position.y))
	
func OnSingleClick(position):
	print("single: "+str(position.x)+","+str(position.y))

MikaelT | 2020-03-17 11:26

:bust_in_silhouette: Reply From: Magso

This is right. Take selecting folders as an example, the first click selects it and if the second click is within the time limit for a double click it opens, thus the log would print single, double and clicking too slowly will print single, single.