|
|
|
|
Reply From: |
Akien |
You could add other inputs for the mouse_click
action, e.g. you should be able to assign a joystick button to act as mouse click. This way you wouldn’t have to handle faking mouse clicks yourself, as both a physical mouse click and a joystick input would be considered as the same actions.
If you do need to send a physical mouse click event from GDScript though, you can do that too. You need to craft an event, and send it. Something like:
var event = InputEvent()
event.type = InputEvent.MOUSE_BUTTON
event.button_index = BUTTON_LEFT
event.pos = selected_button.get_pos() # add here the proper position
get_tree().input_event(event)
(see the docs about InputEvent, InputEventMouseButton and @Global Scope
Alternatively you could also create an InputEventAction, see e.g. the InputEvent tutorial.
This is exactly what I was looking for. I didn’t now how to pass the created InputEvent back into the stack to be processed. I have a screen of sliders and buttons. Using get_tree().input_event(event) still didn’t seem to cause the click on the controls. But I feel I’m on the right track.
zombieCraig | 2016-03-04 22:28
In my above code snippet, I had forgotten to define the position of the mouse click event, I’ve fixed that.
You might also need to set event.pressed = true
, I’m not sure. Check the class reference and experiment
Got to work! You need to send event.pressed = true followed by a false to simulate a “click”
var evt = InputEvent()
evt.type = InputEvent.MOUSE_BUTTON
evt.button_index = BUTTON_LEFT
evt.pos = self.get_global_mouse_pos()
evt.pressed = true
get_tree().input_event(evt)
evt.pressed = false
get_tree().input_event(evt)
zombieCraig | 2016-03-04 22:56
This helped me out a lot! Exactly what I was looking for! Thank you!
For anyone coming across this using Godot 3.2, I found a few minor modifications were needed. The following function worked for me:
func click_the_left_mouse_button():
var evt = InputEventMouseButton.new()
evt.button_index = BUTTON_LEFT
evt.position = get_viewport().get_mouse_position()
evt.pressed = true
get_tree().input_event(evt)
evt.pressed = false
get_tree().input_event(evt)