+5 votes

I followed this tutorial for handling input: http://docs.godotengine.org/en/latest/tutorials/engine/mouse_and_input_coordinates.html?highlight=input

But I only want to do something, if the left mouse button was clicked. How do I check for this?

in Engine by (105 points)

2 Answers

+6 votes
Best answer

I always just use Input.is_action_pressed("nameOfAction") and bind the key or mouse button from the InputMap tab in Project Settings

by (846 points)
selected by

Great! Thank you :)

Hi there, I'm struggling with this. I've gone over the docs.
I just don't know what to put in "nameOfAction".

In the docs I see BUTTON_* as an option. That doesn't work.
I tried MOUSE_BUTTON but nope. I tried BUTTON_1, BUTTON_2 etc. But no luck.

What am I missing here? Thanks so much

EDIT: Cancel that, I'm a goose. Found it.

Instead of just saying you found it you could let others know how:

Project > Project Settings > Input Map (tab)

And then the parameters you can use are "ui_left" etc.

+11 votes

Take a look the documentation for InputEventMouseButton.
You're looking for the button_index property. You can check it like this:

if event.type == InputEvent.MOUSE_BUTTON:
    if event.button_index == BUTTON_LEFT and event.pressed:
        # do something

Of course you can also use InputMap actions for this which is often easier, especially if you want to support multiple input methods (mouse, keyboard, gamepads) for the same gameplay mechanic.

by (924 points)
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.