InputEventMouseButton is fired more than once (Godot 4)

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

Here is a simple code:

func _input(event):
	if event is InputEventMouseButton:
		if event.pressed:
			print('Down')
		else:
			print('Up')

When I clicked down the left button, it’s fired multiple times, sometimes once, but sometimes more than once:

Down
Up
Down
Up
Down

How can I fix this to only fire once?

I can’t replicate this - each input is firing once as expected. Have you tried with a different mouse? Also note that your code will trigger for any mouse button, including double-click events. Are you sure you’re only clicking one button? You can use print(event.as_text()) to double-check what event you’re actually processing.

Also, ou can change the code to

if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:

to ensure you’re just testing one button.

kidscancode | 2023-01-29 17:21

You can try using a:

Input.is_action_just_pressed("here name of your input")

ziomkobra | 2023-01-29 23:50

That would not be correct at all. You shouldn’t be polling Input in _input(), they serve entirely different purposes.

kidscancode | 2023-01-30 04:46

Well, that’s true but I thought about using it out of _input()

ziomkobra | 2023-01-30 09:24

@ziomkobra @kidscancode, none of the above is replicable. Because my mice did it, it’s an old mice that old enough to cause that errors…

Thank you for helping me

thebluetropics | 2023-02-01 04:07

wait…what? I’ve always used Input in _input(). This was following Heartbeast’s tutorials. It’s worked perfectly fine until I switched to Godot 4 where now a single is_input_just_pressed() fires twice each time. I’m not sure what to do about it. Where would I use Input? What would I put in _input()?

PIZZA_ALERT | 2023-06-23 01:12

_input() is called when there’s an event. It is passed that event.

func _input(event):
    if event.is_action_pressed("event_name"):
        # do something
    if event.is_action_released("event_name"):
        # do something else
    # and so on...

This is entirely different from polling the state of the Input singleton, which has nothing to do with an input event. If Heartbeast is doing that, then he’s doing something wrong. It happens - tutorial makers are not always right.

I’d highly recommend looking at the docs to understand this:
Input examples — Godot Engine (stable) documentation in English

kidscancode | 2023-06-23 01:41