How to detect keyboard button double click?

Godot Version

Godot_v4.2.2-stable_mono_win64

Question

I want to make my character run when the right or left arrow keys is pressed twice (double clicked). I tried to do it myself, here’s what I did:

# Defined at the top of the script file
var double_run: float = 0.0
var timer: Timer

# Transition to Run state
	if Input.is_action_just_pressed("left") || Input.is_action_just_pressed("right"):
		double_run += delta * 60
		
		if !timer:
			timer = Timer.new()
			timer.one_shot = true
			timer.wait_time = double_run
			timer.autostart = true
			timer.connect("timeout", delete_timer)
			add_child(timer)
			
		if is_instance_valid(timer):
			if !timer.is_stopped() && Input.is_action_just_pressed("left") || Input.is_action_just_pressed("right"):
                # Change my character state to run at second click
				FSMMachine.change_state(FSMMachine.states["Run"])
			else:
				pass
			

func delete_timer():
	timer.queue_free()

But this doesn’t work as I expected, Instead of it to wait for my second click, it instantly switch to Run state at first click. How can I listen for the arrow keys double click properly? Any help will be apreciated

hi again

is_instance_valid only checks if the timer exists, which it will, since the code is called right after the timer is created. it does not wait for the timeout signal to run

i would just use one timer and restart it when needed instead of making new ones

1 Like

Yoo, Shatteredreality, why are you following me around? lol :joy:

Lemme tweak the is_instance_valid part, I will let you know it it works as I want…

It could help you…!!! good luck!!!

extends Node2D

var secuencia: bool
var contador: int
# Timer 1s One Shot
func _ready():
	$Timer.wait_time = 1
	$Timer.one_shot = true
	contador = 0

func _unhandled_key_input(event):
	#print(event)
	if event.is_released():
		if $Timer.is_stopped():
			$Timer.start()
			contador += 1
		
	if event.is_pressed():
		if !$Timer.is_stopped():
			contador += 1


func _on_timer_timeout():
	print("Fin espera: Contador: ", contador)
	if contador > 4:
		print("Run -> correr")
		
	contador = 0