how can i simulate a mouse click with controller inputs

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

hey, so I’m making a rouglike ship game and when i try to simulate a mouse click when i press a button it doesn’t work maybe you guys can help,
here’s the code I’m relatively new

func click():
    var a = InputEventMouseButton.new()
    a.position = get_global_mouse_position()
    a.set_button_index(MOUSE_BUTTON_LEFT)
    a.button_mask = MOUSE_BUTTON_MASK_LEFT
    a.meta_pressed = true
    a.set_pressed(true)
    print(a)
    Input.parse_input_event(a)

Edited to fix code formatting.

jgodfrey | 2023-04-10 20:12

when i print it i get
InputEventMouseButton: button_index=1, mods=Meta, pressed=true, position=((-359, -4)), button_mask=1

infinity | 2023-04-10 20:29

:bust_in_silhouette: Reply From: Enfyna
func click():
    var a = InputEventMouseButton.new()
    a.position = get_global_mouse_position()
    a.button_index = MOUSE_BUTTON_LEFT
    a.pressed = true
    Input.parse_input_event(a)
    await get_tree().process_frame
    a.pressed = false
    Input.parse_input_event(a)

You have to send a pressed=false event afterwards. If you dont wait for the process_frame signal the pressed=false input is being send twice.

hey i tried it and it still didn’t work i don’t know whats going on but thanks for tring :slight_smile:

infinity | 2023-04-11 20:10

Hmm… This is weird did you try the code i wrote if you did can you make sure no node is blocking your button. Like if you have another node on top you have to set its mouse_filter to ignore. Or can you make sure you are calling click() correctly maybe you are calling it before/after the pointer exited the button. Basically check if the button positions and the button positions match. The code I wrote works on my pc so I dont think the code is at fault here.

Enfyna | 2023-04-11 20:29

it was not set to ignore but even after i set it to ignore it still wasn’t working

but also here is my code for calling that function

	if Input.is_action_pressed("click"):
	call_deferred("click")

infinity | 2023-04-11 20:41

No no the button must be set to stop but if you have a node on top of your button that node needs to be set to ignore. Anyways why do you call click() with call_deferred() I dont think there is any need for that. I would call it straight up like click(). I assume this code is under the _input(event): function if it is I really cant think of anything that is wrong.

Enfyna | 2023-04-11 20:51

the button is set to stop.
and the reason i was ussing call_defferred was because i was looking at code and thats what they had.
the function was being called in _proccess(delta).

and when i did the things you suggested it still didn’t work with controller inputs.
it only works when i press the mouse button.

infinity | 2023-04-11 21:02