How do I simulate a left mouse click? (Godot 3.1 beta)

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

I have this code.

func _click():
   var a = InputEventMouseButton.new()
   a.set_button_index(1)
   a.position = Vector2(520,250)
   a.pressed = true
   get_tree().input_event(a)

func _input(event):
   if Input.is_action_just_pressed("ui_up"):
	   _click()

But it does not work.
I would be glad if someone can help.

:bust_in_silhouette: Reply From: wombatstampede

The Method parse _ input _ event is supposed to do this:

Input.parse_input_event(a)

See:

…and:

Thanks for your answer.
Unfortunately I get this error when using it.

 drivers/unix/net_socket_posix.cpp:190 - Socket error: 10054

Did I something wrong?
My code:

func _click():
var a = InputEventMouseButton.new()
a.set_button_index(1)
a.set_pressed(true)
Input.parse_input_event(a)

Adam_S | 2019-01-21 16:41

The error message looks more like the debugger lost the connection to your app. This mostly happened when the app/program fails. (Without a proper error message.)

Maybe it is problematic calling “parse _ input _ event” just at that moment. (I.e. when handling another event)

You could try call_deferred:

 func _click():
     call_deferred("do_a_left_click")

func do_a_left_click:
     var a = InputEventMouseButton.new()
     a.set_button_index(1)
     a.set_pressed(true)
     Input.parse_input_event(a)

wombatstampede | 2019-01-21 17:04

It works when using “call_deferred()”.

Thanks a lot for your help!!!

Adam_S | 2019-01-21 17:30