InputEventKey in Godot – Complete Guide

Aspiring game developers often find themselves entangled in the intricate world of handling user inputs, and understanding various classes and methods associated with them can be quite the challenge. Nonetheless, mastering input events is a fundamental step in creating engaging and interactive gameplay in Godot Engine. In this tutorial, we shall explore the `InputEventKey` class in Godot 4, an integral part of the input system that allows us to respond to keyboard events. Whether you’re building a fast-paced action game, a strategy game requiring hotkeys, or any interactive project, comprehending `InputEventKey` will arm you with the ability to capture and process keyboard inputs effectively.

What is InputEventKey?

The `InputEventKey` class in Godot is an object that encapsulates information related to keyboard inputs. It extends from `InputEventWithModifiers`, which means it not only carries data about the key event but also about any modifier keys that may be held down (like Shift or Control). In essence, `InputEventKey` holds the details for key presses and releases, making it easier for developers to implement responsive controls for their games.

What is it for?

Using the `InputEventKey`, you can detect when a user has pressed or released a key on their keyboard within your game. This detection can be used to trigger various types of actions or behaviors, from moving a character to opening a menu. Recognizing keys and their state is fundamental for creating intuitive and seamless gameplay experiences.

Why Should I Learn About It?

Learning about `InputEventKey` can significantly improve the way you manage user input in your Godot projects. Being adept at handling keyboard events means you can:

– Implement complex control schemes.
– Improve the interactivity of your games.
– Cater to different keyboard layouts and input methods.

Understanding `InputEventKey` is not just useful but crucial for creating professional-quality games that feel good to play. Now, let’s dive in and start playing with some code examples to see how `InputEventKey` works in practice.

CTA Small Image

Handling Basic Key Events

To handle basic key events in Godot using the `InputEventKey` class, we’ll start with detecting a single key press. The snippet below demonstrates how to respond to an “A” key press in the `_input` function.

func _input(event):
    if event is InputEventKey:
        if event.pressed and event.scancode == KEY_A:
            print("The 'A' key was pressed.")

Next, let’s check for a key release. The logic is similar to a key press, but this time we’re interested in when the key is no longer pressed.

func _input(event):
    if event is InputEventKey:
        if not event.pressed and event.scancode == KEY_A:
            print("The 'A' key was released.")

Another essential concept is detecting whether a key is held down. To achieve this, we can use the `_process` function with the `Input.is_key_pressed` method.

func _process(delta):
    if Input.is_key_pressed(KEY_A):
        print("The 'A' key is being held down.")

Handling modifiers is also crucial. Let’s check for the “A” key press alongside the “Control” modifier:

func _input(event):
    if event is InputEventKey:
        if event.pressed and event.scancode == KEY_A and event.control:
            print("The 'A' key was pressed with 'Control'.")

Implementing Character Movement

Moving a character using `InputEventKey` involves monitoring the arrow keys or WASD keys. Here’s an example using WASD for movement in 2D space.

var velocity = Vector2.ZERO

func _process(delta):
    velocity = Vector2.ZERO
    if Input.is_key_pressed(KEY_W):
        velocity.y -= 1
    if Input.is_key_pressed(KEY_S):
        velocity.y += 1
    if Input.is_key_pressed(KEY_A):
        velocity.x -= 1
    if Input.is_key_pressed(KEY_D):
        velocity.x += 1
    velocity = velocity.normalized() * speed
    position += velocity * delta

This will create a basic 8-directional movement system. Notice how we normalize the velocity to prevent faster diagonal movement, which is a common pitfall in input handling.

Creating Hotkeys and Shortcuts

Hotkeys or shortcuts are often used to provide quick access to features. Below is an example of how to create a simple hotkey for opening an in-game menu.

func _input(event):
    if event is InputEventKey:
        if event.pressed and event.scancode == KEY_ESCAPE:

A more advanced hotkey may include modifiers like so:

func _input(event):
    if event is InputEventKey:
        if event.pressed and event.scancode == KEY_S and event.control:
            print("Saving the game...")

By grasping the basics of `InputEventKey`, you can begin to see just how powerful and flexible Godot’s input system can be for creating responsive and interactive games. Stay tuned for the next part where we’ll delve into more complex examples and use cases!In this section, we’ll expand on our knowledge of `InputEventKey` and explore more complex examples of how to use this class to enhance your Godot games. From handling multiple key combinations to creating a custom key binding system, the versatility of key input events in Godot is one of the reasons why it’s such a powerful tool for game developers.

Advanced Key Combination Detection

Detecting combination key presses can be useful for executing special moves or accessing hidden features. Here’s how you can detect a combination of three keys being pressed together:

func _input(event):
    if event is InputEventKey and event.pressed:
        if event.scancode == KEY_G and event.shift and event.alt:
            print("Detected a special G key combination with Shift and Alt!")

This kind of detection can be the basis for cheat codes, special abilities, and more complex game interactions.

Custom Key Bindings

Allowing players to customize their key bindings can greatly enhance the game experience. Here’s an elementary way to let the player set their own key for moving forward:

var forward_key = KEY_W

func _input(event):
    if event is InputEventKey and event.pressed:
        if event.scancode == forward_key:
            print("Move forward")

To change the key binding, you could use a function like this:

func set_forward_key(new_key):
    forward_key = new_key

This simple system can be expanded to cover all game controls and even saved to a file for persistence across gameplay sessions.

Input Buffering for Combinations

Input buffering is a technique often used in fighting games to register a series of inputs as a combo. Here’s a very basic example of input buffering:

var input_buffer = []

func _input(event):
    if event is InputEventKey and event.pressed:
        # Start a timer to clear input_buffer if inputs stop
func _process(delta):
    # Check buffer for specific combos
    if input_buffer == [KEY_W, KEY_W, KEY_S]:
        print("Secret combo move activated!")

Note that for practical use, you would need to consider timing and sequence detection, which is beyond the scope of this overview. Input buffering systems need to be well-thought-out and tested extensively.

Soft and Hard Key Repeat

While Godot automatically handles key repeats through operating system settings, you may want to control the repeat rate or create a “soft” repeat (repeats triggered after a delay) for certain actions. Here’s a basic scheme for soft key repeat:

var repeat_timer = 0.5 # Half a second before the key repeats
var time_since_last_press = 0.0

func _process(delta):
    if Input.is_key_pressed(KEY_A):
        if time_since_last_press == 0.0 or time_since_last_press > repeat_timer:
            print("Soft repeat triggered for 'A' key")
            time_since_last_press = 0.0 # Reset the timer after the action
        time_since_last_press += delta
        time_since_last_press = 0.0 # Reset if key is not being pressed

In this example, the action tied to pressing the “A” key will only execute once every half second, regardless of how long the key is held down. This can be useful for certain game mechanics where you want controlled repetition.

Debouncing Key Inputs

Debouncing is important when you want to ensure that a key press only triggers an action once per press, even if the key was bounced (rapidly pressed unintentionally). This simple debounce check can be implemented as follows:

var last_key_pressed = ""
var debounce_time = 0.2
var time_since_last_key = 0.0

func _process(delta):
    time_since_last_key += delta

func _input(event):
    if event is InputEventKey and event.pressed:
        if event.as_text() != last_key_pressed or time_since_last_key > debounce_time:
            last_key_pressed = event.as_text()
            time_since_last_key = 0.0
            # Trigger your action here
            print("Debounced key press: ", last_key_pressed)

By managing the time since the last key press, you can ensure that an accidental rapid press doesn’t trigger multiple actions unintentionally.

Through these examples, you should now have a substantial understanding of how to leverage `InputEventKey` to create responsive and sophisticated controls in your Godot projects. Remember, refining input handling can drastically improve the player experience, so take the time to craft your system thoughtfully and test it thoroughly. With Godot’s powerful input event system at your disposal, the only limit is your imagination. Happy developing!As you dive deeper into the world of game creation with Godot, you’ll find that handling inputs can become quite intricate. This section will guide you through additional code examples that will give you an even broader understanding of managing key events, such as implementing state machines for character control and creating customizable control schemes.

State machines are often used in game development to manage complex behaviors. Consider a character that can walk, run, or sprint based on different key inputs:

var current_state = States.IDLE

func _process(delta):
    match current_state:
            if Input.is_key_pressed(KEY_W): 
                current_state = States.WALKING
            if not Input.is_key_pressed(KEY_W):
                current_state = States.IDLE
            elif Input.is_key_pressed(KEY_SHIFT):
                current_state = States.RUNNING
            if Input.is_key_pressed(KEY_CONTROL):
                current_state = States.SPRINTING
            elif not Input.is_key_pressed(KEY_SHIFT):
                current_state = States.WALKING
            if not Input.is_key_pressed(KEY_CONTROL):
                current_state = States.RUNNING

You can use this setup to trigger different animations or movement speeds according to the `current_state` your character is in.

Creating a dynamic and customizable control scheme is also a great way to improve your game’s accessibility and user experience. An example could be using a dictionary to map actions to keys:

var key_mapping = {
    "move_left": KEY_A,
    "move_right": KEY_D,
    "jump": KEY_SPACE,
    "shoot": KEY_K

func _input(event):
    if event is InputEventKey and event.pressed:
        for action in key_mapping:
            if event.scancode == key_mapping[action]:
                call(action + "_pressed")
func move_left_pressed():
    # Code for moving left
func move_right_pressed():
    # Code for moving right
func jump_pressed():
    # Code for jumping
func shoot_pressed():
    # Code for shooting

For more advanced systems, you might want to handle multiple key events for combos or sequences. Here’s an example of using an array to keep track of the last few pressed keys:

var key_sequence = []
var key_sequence_length = 4

func _input(event):
    if event is InputEventKey and event.pressed:
        if key_sequence.size() > key_sequence_length:

func check_combos():
    if key_sequence.back() == KEY_J and key_sequence.size() == key_sequence_length:
        if key_sequence[0] == KEY_W and key_sequence[1] == KEY_W and key_sequence[2] == KEY_S:
            print("Performed a jump attack!")

This can be refined by adding timing checks, but this snippet sets up the basic structure for sequence detection.

For games requiring precision and rhythm, distinguishing between short and long key presses becomes necessary:

var press_start_time = 0.0

func _input(event):
    if event is InputEventKey:
        if event.pressed:
            press_start_time = OS.get_ticks_msec()
        elif event.scancode == KEY_L and not event.pressed:
            var duration = OS.get_ticks_msec() - press_start_time
            if duration < 250:
                print("Short press")
                print("Long press")

In time-critical games such as music rhythm or quick reaction titles, having precise control over input timing is crucial. This code differentiates between a quick tap and a hold based on the time in milliseconds.

Lastly, you might also want to implement a system that allows users to hold down a key to repeatedly trigger an action at a fixed interval:

var auto_repeat_rate = 0.2 # Time in seconds
var auto_repeat_timer = 0.0

func _process(delta):
    if Input.is_key_pressed(KEY_F):
        auto_repeat_timer += delta
        if auto_repeat_timer >= auto_repeat_rate:
            auto_repeat_timer = 0.0 # Reset timer
            fire_projectile() # Replace with your action

func fire_projectile():
    # Implement projectile firing logic here

In this case, as long as the key is pressed, `fire_projectile` will be called at intervals of `auto_repeat_rate`.

By expanding your repertoire of input handling techniques, you can create a more nuanced and engaging player interaction within your game. The examples provided here are just the beginning; there is much more you can do with Godot’s input system. Developing a good understanding of these concepts will go a long way in polishing the feel of your game and ensuring a smooth player experience.

Continue Your Game Development Journey

Diving into the world of game development with Godot is both exciting and rewarding. Now that you’ve begun mastering inputs with the `InputEventKey` class, the birth of your own interactive worlds is at your fingertips. But don’t stop here – there’s an entire cosmos of knowledge awaiting your discovery as you strive to turn your game concepts into reality. We encourage you to keep learning and refining your skills in game development with our comprehensive Godot Game Development Mini-Degree. This series of expertly crafted tutorials is a treasure trove of essential skills and practical examples that you can use to build cross-platform games using the latest iteration, Godot 4.

Whether you’re new to the field or ready to level up your existing developer toolkit, our Mini-Degree will guide you through the creation of 2D and 3D games with hands-on projects across various genres. These projects are your gateway to building a robust portfolio; looking great when you showcase your capabilities to prospective clients or employers. Boost your career with our flexible learning options that include live coding sessions, interactive quizzes, and a plethora of other educational content.

If you’re eager for more, explore our broader collection of Godot courses. Each course is designed to take you step by step through the game development process, all the way from beginner to professional. At Zenva, our mission is to provide you with the tools and knowledge to create your own games, launch a career in the industry, or start your own game development venture. Embark on this journey with us, and let your creativity soar.


Embracing the facets of `InputEventKey` in Godot is more than just learning a new skill; it’s about unlocking limitless creative potential in the realm of interactive design. The knowledge you’ve garnered today is a small fragment of the vast expanse that game development offers. We take pride in having guided you through this gateway, and we extend a hand for your next bold strides. With every line of code and every project, you are shaping the experiences of players around the world, bringing to life stories and adventures through the games you develop. This is the essence of what it means to become a game maker.

We invite you to not only continue but to deepen your learning journey with us. Our Godot Game Development Mini-Degree awaits as your next stepping stone towards mastery. Remember, every great game begins with a simple line of code, and each lesson propels you forward in bringing your visions to life. We’re thrilled to be part of your story, and can’t wait to see where your newfound insights will take you. Keep coding, keep creating, and let’s build incredible gaming experiences together!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.