0 votes

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
in Engine by (25 points)

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

1 Answer

+1 vote
Best answer

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.

by (3,227 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.