Distinguish one click from double clicks

Godot Version

4.7

Question

Hello i’d like to know how would i be able to make godot figure out between double click and a single click, i’m currently in the process of making a fake OS and i’ve made the shortcuts in the desktop i managed to add the placement logic but i’d like to know how would i do the opening logic
it should work the same as windows or linux, double click on an icon and it will immediately open up a window, problem is i already use mouse 1 to allow the ability to move the icon across the desktop

func _handel_placement() -> void:
	if Input.is_action_pressed("mouse_1"):
		if get_global_rect().has_point(mouse_pos):
			is_grabbed = true
	
	if Input.is_action_just_released("mouse_1"):
		is_grabbed = false
	if is_grabbed:
		global_position = mouse_pos - (size /2)
	else:
		global_position = global_position.snapped(SNAP_VALUE)
		global_position = global_position.clamp(SNAP_VALUE, Vector2(get_viewport().size) - SNAP_VALUE)

This is the code i have for placement simple yet it works, the thing is i want the engine to be able to detect double click and open the app and if it was one click and hold it does the logic above

InputEventMouseButton.double_click contains that information for mouse events. InputEventScreenTouch.double_tap has it for touch events.

You could move your input handling information into any of the _input() methods there are. Check the documentation here to choose which one you will need:

If you are emulating the mouse with a gamepad or keyboard then you’ll need to manually code the logic to deal with a simulated double click.

that looks like exactly what i need, but i couldn’t figure out how to use it

Try this:

func _unhandled_input(event: InputEvent) -> void:
    if event.is_action_pressed("mouse_1") and event.double_click:
        print("You double clicked!")

For supporting controllers too, you can create a timer variable and add delta to it in the _physics_process(). Each time you press the desired button, you check if the timer is below a certain threshold, if it is, you trigger double click. It can look something like this:

var start_timer: bool = false
var clicked_once: bool = false
var time: float = 0
var double_click_time_limit: float = 0.2

func _unhandled_input(event: InputEvent) -> void:
   ## Assuming you use "mouse_1" input action for gamepads too
	if event.is_action_pressed("mouse_1"):
		start_timer = true
		if time <= double_click_time_limit and clicked_once:
			print("You double clicked!")
			time = 0
			start_timer = false
			clicked_once = false
			## Insert other functions here for what you wanna do with double click
		else:
			clicked_once = true


func _physics_process(delta: float) -> void:
	if start_timer:
		time += delta
		if time > double_click_time_limit:
			time = 0
			clicked_once = false
			start_timer = false

Can’t double click be mapped to an action?

I would’ve expected that to be available already… :thinking:

Don’t have the laptop :laptop: at the moment, so hard to verify… Is there a ticket open for it yet that you know of? Or a very valid reason for it not to be erm .. ‘actionable’? :grin::wink:

Loving that idea.

func your_function() -> void:
  if Input.is_action_pressed("mouse_1"):
      clicks += 1
  If clicks == 2:
     clicks = 0
     is_grabbed = true

This is an example that ive done on my game. Just create var and check if ths clicks are 2 or not. If they are 2 then they become 0 and the var is_grabbed is true unless you release the input. If its only pressed one time just use do if clicks = 1. I do it a lot in my games because its easier and sometimes the methods the others use they could not work for some reason.

This works, but for some reason when i double click it also opens other icon’s window as well maybe because it’s _unhandled_input so it capture the mouse inputs regardless i i have the mouse on the icon or not so i guess i need to check if the mouse is on top of the icon first before dbl pressing, thanks for the answer

surprisingly it does although detect double click but it won’t work for some reason, i have no idea why

really simple i think this works the best in my case tbh

I guess because both actions were in a ’ just pressed’ stare at the same time then.

Whatever works best for you, I guess

both Vachivenza’s and realkris’s solutions works really well

Happy to help, cheers!