How can I check to see if any key on the keyboard has been pressed?

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

I want to print something if any key is pressed on the keyboard except the enter key. How can I do that? With InputEvent == InputEventKey?

:bust_in_silhouette: Reply From: SIsilicon

You almost got it.

Best to detect in the input function.

func _input(event):
    if event is InputEventKey and event.pressed:
        if event.scancode != KEY_ENTER:
            #Do what you gotta do.

If you wanna know what key you are pressing, then that’s what that scancode variable in the second if is. However if you print it out as it is it would return an integer.

#Eg: If I pressed A then this would print 64.

That integer is a KeyList enumerator in GlobalScope. Which means you have to compare it against one of those constants to know if it’s that key in particular.

#Eg: Now this would print true if I pressed A.
print(event.scancode == KEY_A)

There are more that you can find here under Enums > KeyList.

Oh and by the way, a question like that would more closely fit Engine category.

SIsilicon | 2018-05-18 02:04

Thanks for the help!

aagoldberg | 2018-05-18 13:18

For anyone looking on google like me, there are two particular methods for godot 4 and godot 3 respectively:Godot 4.2 has a unique function called Input.is_anything_pressed(), it is a boolean that returns true if there’s an input and false if theres no input.


For Godot 3.x, I used a hacky workaround.

if (Input.get_vector("move_left","move_right","move_forward","move_backward"))!=Vector2.ZERO):

if (Input.get_axis("move_left","move_right"))!=0.0: