Better way to know which mousebutton pressed the button

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

Hi all,

i have the problem that i need to know which mouse-button pressed a Button.

I have this solution:

extends Control

var last_mouse_button : int = -1
var mouse_buttons : Array = ["None", "Left", "Right", "Middle"]

func _input(event : InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.pressed:
			last_mouse_button = event.button_index

func _on_Button_pressed() -> void:
	print(mouse_buttons[last_mouse_button])

but i dont think its very elegant and i dont know how reliable this is…
Does anyone know a better solution?

:bust_in_silhouette: Reply From: Wakatta

Ok so first you’ll want to use func _gui_input(event) as _input captures from everywhere not just your button.

Then use either button_mask or button_index with ButtonList

No need to reinvent the wheel the way you have

func  _gui_input(event : InputEvent) -> void:
    if event is InputEventMouseButton:
        if event.pressed:
            if event.button_mask == BUTTON_RIGHT:
                pass
            if event.button_index == BUTTON_LEFT:
                pass