Godot Keyboard Events
Asked Answered
K

8

15

I am studying the Godot Engine and GDScript and I searched on the internet about keyboard events, but I didn't understand. Is there something in Godot like: on_key_down("keycode")?

Known answered 4/9, 2017 at 22:37 Comment(0)
E
16

You can use InputEvent to check for specific Keys.

Check out the documentation: http://docs.godotengine.org/en/stable/learning/features/inputs/inputevent.html

Empoison answered 5/9, 2017 at 7:53 Comment(1)
Is there anything specific for key down, key up and just key?Known
S
19

Godot versions 3.0 and up have new input-polling functions that can be used anywhere in your scripts:

  • Input.is_action_pressed(action) - checks if the action is being pressed
  • Input.is_action_just_pressed(action) - checks if the action was just pressed
  • Input.is_action_just_released(action) - checks if the action was just released
Shae answered 21/4, 2018 at 18:19 Comment(0)
E
16

You can use InputEvent to check for specific Keys.

Check out the documentation: http://docs.godotengine.org/en/stable/learning/features/inputs/inputevent.html

Empoison answered 5/9, 2017 at 7:53 Comment(1)
Is there anything specific for key down, key up and just key?Known
C
8

There's no official OnKeyUp option, but you can use the _input(event) function to receive input when an action is pressed/released:

func _input(event):

    if event.is_action_pressed("my_action"):
        # Your code here
    elif event.is_action_released("my_action):
        # Your code here

Actions are set in Project Settings > Input Map.

Of course you don't always want to use _input, but rather get inputs within the fixed updates. You can use Input.is_key_pressed() but there's no is_key_released(). In that case you can do this:

var was_pressed = 0

func _fixed_process(delta):
    if !Input.is_key_pressed() && was_pressed = 1:
        # Your key is NOT pressed but WAS pressed 1 frame before
        # Code to be executed

    # The rest is just checking whether your key is just pressed
    if Input.is_key_pressed():
        was_pressed = 1
    elif !Input.is_key_pressed():
        was_pressed = 0

That's what I've been using. Feel free to inform me if there's a better way to do OnKeyUp in Godot.

Chronological answered 11/12, 2017 at 4:3 Comment(0)
I
0

If you're considering using Input or _Input(event), it's important you go into your project settings and bind your keys.

Internment answered 4/10, 2021 at 18:4 Comment(0)
H
0

Press project setting in the toolbar, go to input map and then you can name an action and add any key, mouse or joystick to it. The in the code use:

if Input.is_action_just_pressed('Your action name'):
   print('Pressed!')

where is project settings button

Handiness answered 4/1, 2022 at 13:29 Comment(0)
M
0

There are multiple ways to register keys:

-is_key_pressed("Enter")
-Input.is_action_pressed(*action*)

I would use Input.is_action_pressed() because it allows you to work with the input manager.

Monkhmer answered 12/6, 2022 at 7:47 Comment(0)
L
0

There is no _on_key_pressed() function in godot. Instead godot stores certain action e.g.- shoot, walk, jump as Input Maps which can be bound to certain keys (can be more than one). Then to detect the action in code there are three functions:

  1. Input.is_action_just_pressed(NameOfAction) - a single key press
  2. Input.is_action_pressed(NameOfAction) - detects if key is being held down
  3. Input.is_action_just_pressed(NameOfAction) - detects if key is released

These functions can be used to detect player input in a script

NOTE- there are a bunch of default ui actions already stored in every godot project like ui_up for up arrow. If you want custom actions or inputs then you can go to the Project Settings -> Input Maps and add the actions and add the specific key, mouse click or joystick movement.

SAMPLE PROJECT:

  1. Open a new project

  2. Go to Project Settings -> Input Maps

  3. In the text box type shoot and hit ENTER or click ADD

  4. Add the spacebar to this action

  5. Add a new scene with a script

  6. In the script write-

    if Input.is_action_pressed("shoot"):
        #Code for shoot mechanic
        pass
    
Lueck answered 14/6, 2023 at 10:31 Comment(0)
L
0

Something along the lines of:

if Input.is_action_just_pressed("key"):
    #what you need to do

should work; however, you need to reconfigure the input map by looking on the top bar (the bar at the top of the screen on Mac), selecting "project" , a popup appears, select "project settings", and select "input map".

Then, you can add an action (name matters, i.e. "left","right", whatever you put into the parameter "key", etc), click the plus arrow, and press whatever key you need to link.

projects tab link key inputs

Hope this helps!

Luedtke answered 18/6 at 1:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.