+5 votes

How do I ignore the key repeats/echos that occur in input handling, when holding a key?

asked Feb 22, 2016 in Engine by Calinou (6,560 points)

1 Answer

+4 votes
Best answer

Instead of using this code:

func _input(event):
    if Input.is_action_pressed("say_hi"):
        print("Hi!")

Use this:

func _input(event):
    if event.is_action_pressed("say_hi"):
        print("Hi!")

Using the input event directly is generally a good idea, as it is less prone to issues that can happen when eg. there also is mouse input in the game.

answered Feb 22, 2016 by Calinou (6,560 points)
Let me add an info, that if the subject would be about screen buttons like an in games GUI, you may also use TouchScreenButton and check emulate touchscreen in project settings. TouchScreenButtons do not have a problem of repeats and they work on mobile devices well.
Also if you're parsing input events directly instead of using actions, there are:
    event.is_pressed()
and
    event.is_echo()
Input.is_action_pressed() is called polling and  can be run anywhere in the code
in fact i think you don't even need to use _input(event) for event based although it does make it easier   for events.
im pretty sure if you do set_process_input(true)  in _ready then you can call the Input and InputEvent singletons directly from anywhere
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.