0 votes

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?

Godot version 3.2.3
in Engine by (1,448 points)

1 Answer

0 votes
Best answer

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
by (4,476 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.