How to prevent redoing action when key is holded instead of single pressed

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

In the _input(event): I have if Input.is_action_pressed("menu"): (menu is bound to the Esc key) to show the menu when I press Esc (it simply .show() a controll node (the menu), if the menu controll node .is_visible() == false or .hide() the menu controll node, if the menu controll node .is_visible() == true) and hold it, it open and closes the menu (and so on) continiously.
I don’t want that, I would prefer that it only shows/hides the menu once, regardless of whether I pressed Esc once or hold it.

Is there a way of doing it? Or do I need to make a “workaround” with Input.is_action_just_released()?

Thank you very much!

:bust_in_silhouette: Reply From: kidscancode

Don’t use the Input singleton in _input(). You already have a reference to an individual input: event.

func _input(event):
    if event.is_action_pressed("menu"):
        visible = not visible

This will only fire once when you press the key.

For more about how to use inputs, and the difference between Input and _input(), please read: Input examples — Godot Engine (3.2) documentation in English