Input detection problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tam0705
:warning: Old Version Published before Godot 3 was released.

I have a problem in input detection.
When I pressed a key (for example, left key), the input correctly detected that the key is pressed, but when then I pressed another key (for example, up key) without releasing the previous key, the input only detected the up key. The left key was only being detected again when I released the left key.
Did I do something wrong? I’d tried to print and see what the input detected, and I was right. Here is the input script I’m using:

func _input(event):
    if event.is_action("ui_left"):
        #go left
    elif event.is_action("ui_right"):
        #go right
    if event.is_action_pressed("ui_up"):
        #jump!
    pass

I had also tried to change the script by separating the up key detection in another node, but still having the issue I said in the beginning.
Sorry for my bad English :slight_smile:

Thank you so much! Now the problem has been solved :slight_smile:

tam0705 | 2017-05-12 09:55

:bust_in_silhouette: Reply From: Zylann

_input is only called once per input event, and the event variable will only concern that particular input. This is why you receive left and up individually.

If you want to check both, you should remember the key state in a boolean variable, or use Input.is_action_pressed("ui_right"), or check input every frame in _process.

The only problem I see with that is that checking every frame is kind of bumpy, and even in that case the inputs will be checked in every loop in a certain order, so interference is to be expected.

I also found that using elif in _fixed_process doesn’t allow two keys to be active at once, only works with if alone. Which is weird, since it wasn’t like that in previous versions. :expressionless:

rredesigns | 2017-05-10 16:32

Using elif will check only one of the two branches, because that’s how the programming language is built (and every other as well).
If you want to check multiple keys, just write if lines and they will be checked one by one regardless of the state of the others:

if key_1:
    do stuff
if key_2:
    do stuff
etc...

Also it’s fine to check every frame, that’s how many games do to move characters in real time. You can do it with _input by managing a set of booleans but in the end you need to convert them into movement anyways.

Zylann | 2017-05-10 20:11