Button pressed function running on its own in another function

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

Hi,
I faced this really strange challenge with gdscript. I don’t know if I really can’t see the problem in this simple code or this is some kind of bug.
Here’s the code where I want to increase the counter by pressing enter and then reducing it by pressing the button. Strange thing happens when I press enter two or three times, then reduce the counter by pressing the button. When I press enter once again, it runs the button function automatically after it. The counter gets up by 1 and drops by 1 just by pressing enter. I can’t see any connection between the two, but maybe it’s just me. Hopefully someone can try the code and help me understand what is going on here.

extends Node2D
var counter = 0

func _input(event):
	if event.is_action_released("ui_accept"):
		counter = counter + 1
		print("Space pressed")
		print(counter)

func _on_Button_pressed():
	counter = counter - 1
	print("Button pressed")
	print(counter)

UPDATE: Using different signal func _on_Button_gui_input(event) with the same code works as intended, but I still don’t see the real reason why pressed signal is causing the problem in the first place.

:bust_in_silhouette: Reply From: jgodfrey

I’d guess this is a focus issue. That is, when you click on the button with the mouse, the app’s “focus” shifts to that button. From the point on, when you press Enter, it presses the button because it has focus. That behavior is controlled by the button’s enabled_focus_mode property. If you set that to None, the button will no longer take focus and pressing Enter will no longer cause it to be pressed.

Exactly. I knew it’s something simple.
Thank you very much!

Happyman | 2023-01-16 10:47