Input examples

Introduction

InputEvent system to capture player input. There are many different types of input your game may use - keyboard, gamepad, mouse, etc. - and many different ways to turn those inputs into actions in your game. This document will show you some of the most common scenarios, which you can use as starting points for your own projects. Note Using InputEvent.

Events versus polling

_input() function, which will be called whenever an input event occurs. In the second case, Godot provides the Input singleton, which you can use to query the state of an input. Examples: GDScript C#

  1. func _input(event): if event.is_action_pressed("jump"): jump()func _physics_process(delta): if Input.is_action_pressed("move_right"): # Move as long as the key/button is pressed. position.x += speed * delta
  1. public override void _Input(InputEvent inputEvent){ if (inputEvent.IsActionPressed("jump")) { Jump(); }}public override void _PhysicsProcess(float delta){ if (Input.IsActionPressed("move_right")) { // Move as long as the key/button is pressed. position.x += speed * delta; }}

This gives you the flexibility to mix-and-match the type of input processing you do. _input().

Input events

InputEvent. Depending on the event type, the object will contain specific properties related to that event. To see what events actually look like, add a Node and attach the following script: GDScript C#

  1. extends Nodefunc _input(event): print(event.as_text())
  1. using Godot;using System;public class Node : Godot.Node{ public override void _Input(InputEvent inputEvent) { GD.Print(inputEvent.AsText()); }}

As you press keys, move the mouse, and perform other inputs, you’ll see each event scroll by in the output window. Here’s an example of the output:

  1. AInputEventMouseMotion : button_mask=0, position=(108, 108), relative=(26, 1), speed=(164.152496, 159.119843), pressure=(0), tilt=(0, 0)InputEventMouseButton : button_index=BUTTON_LEFT, pressed=true, position=(108, 107), button_mask=1, doubleclick=falseInputEventMouseButton : button_index=BUTTON_LEFT, pressed=false, position=(108, 107), button_mask=0, doubleclick=falseSFAltInputEventMouseMotion : button_mask=0, position=(108, 107), relative=(0, -1), speed=(164.152496, 159.119843), pressure=(0), tilt=(0, 0)

InputEventMouseButton. It inherits from the following classes:

  • InputEvent - the base class for all input events
  • InputEventWithModifiers - adds the ability to check if modifiers are pressed, such as Shift or Alt.
  • InputEventMouse - adds mouse event properties, such as position
  • InputEventMouseButton - contains the index of the button that was pressed, whether it was a double-click, etc. Tip It’s a good idea to keep the class reference open while you’re working with events so you can check the event type’s available properties and methods. position on InputEventKey for example. To avoid this, make sure to test the event type first: GDScript C#
    1. func _input(event): if event is InputEventMouseButton: print("mouse button event at ", event.position)
    1. public override void _Input(InputEvent inputEvent){ if (inputEvent is InputEventMouseButton mouseEvent) { GD.Print("mouse button event at ", mouseEvent.Position); }}

    InputMap

    InputMap is the most flexible way to handle a variety of inputs. You use this by creating named input actions, to which you can assign any number of input events, such as keypresses or mouse clicks. A new Godot project includes a number of default actions already defined. To see them, and to add your own, open Project -> Project Settings and select the InputMap tab:

    Capturing actions

    is_action_pressed() and is_action_released() by passing the name of the action you’re looking for: GDScript C#
    1. func _input(event): if event.is_action_pressed("my_action"): print("my_action occurred!")
    1. public override void _Input(InputEvent inputEvent){ if (inputEvent.IsActionPressed("my_action")) { GD.Print("my_action occurred!"); }}

    Keyboard events

    InputEventKey. While it’s recommended to use input actions instead, there may be cases where you want to specifically look at key events. For this example, let’s check for the T: GDScript C#
    1. func _input(event): if event is InputEventKey and event.pressed: if event.scancode == KEY_T: print("T was pressed")
    1. public override void _Input(InputEvent inputEvent){ if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed) { if ((KeyList)keyEvent.Keycode == KeyList.T) { GD.Print("T was pressed"); } }}
    Tip @GlobalScope_KeyList for a list of scancode constants. Warning keyboard ghosting, not all key inputs may be registered at a given time if you press too many keys at once. Due to their location on the keyboard, certain keys are more prone to ghosting than others. Some keyboards feature antighosting at a hardware level, but this feature is generally not present on low-end keyboards and laptop keyboards. this Gamedev Stack Exchange question for more information.

    Keyboard modifiers

    InputEventWithModifiers. This allows you to check for modifier combinations using boolean properties. Let’s imagine you want one thing to happen when the T is pressed, but something different when it’s Shift + T: GDScript C#
    1. func _input(event): if event is InputEventKey and event.pressed: if event.scancode == KEY_T: if event.shift: print("Shift+T was pressed") else: print("T was pressed")
    1. public override void _Input(InputEvent inputEvent){ if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed) { switch ((KeyList)keyEvent.Scancode) { case KeyList.T: GD.Print(keyEvent.Shift ? "Shift+T was pressed" : "T was pressed"); break; } }}
    Tip @GlobalScope_KeyList for a list of scancode constants.

    Mouse events

    InputEventMouse class, and are separated into two types: InputEventMouseButton and InputEventMouseMotion. Note that this means that all mouse events will contain a position property.

    Mouse buttons

    @GlobalScopeButtonList contains a list of `BUTTONconstants for each possible button, which will be reported in the event’sbutton_indexproperty. Note that the scrollwheel also counts as a button - two buttons, to be precise, with bothBUTTON_WHEEL_UPandBUTTON_WHEEL_DOWN` being separate events. GDScript *C#
    1. func _input(event): if event is InputEventMouseButton: if event.button_index == BUTTON_LEFT and event.pressed: print("Left button was clicked at ", event.position) if event.button_index == BUTTON_WHEEL_UP and event.pressed: print("Wheel up")
    1. public override void _Input(InputEvent inputEvent){ if (inputEvent is InputEventMouseButton mouseEvent && mouseEvent.Pressed) { switch ((ButtonList)mouseEvent.ButtonIndex) { case ButtonList.Left: GD.Print("Left button was clicked at ", {mouseEvent.Position}); break; case ButtonList.WheelUp: GD.Print("Wheel up"); break; } }}

    Mouse motion

    InputEventMouseMotion events occur whenever the mouse moves. You can find the move’s distance with the relative property. Sprite node: GDScript C#
    1. extends Nodevar dragging = falsevar click_radius = 32 # Size of the sprite.func _input(event): if event is InputEventMouseButton and event.button_index == BUTTON_LEFT: if (event.position - $Sprite.position).length() < click_radius: # Start dragging if the click is on the sprite. if not dragging and event.pressed: dragging = true # Stop dragging if the button is released. if dragging and not event.pressed: dragging = false if event is InputEventMouseMotion and dragging: # While dragging, move the sprite with the mouse. $Sprite.position = event.position
    1. using Godot;using System;public class Node2D : Godot.Node2D{ private bool dragging = false; private int clickRadius = 32; // Size of the sprite. public override void _Input(InputEvent inputEvent) { Sprite sprite = GetNodeOrNull<Sprite>("Sprite"); if (sprite == null) { return; // No suitable node was found. } if (inputEvent is InputEventMouseButton mouseEvent && (ButtonList)mouseEvent.ButtonIndex == ButtonList.Left) { if ((mouseEvent.Position - sprite.Position).Length() < clickRadius) { // Start dragging if the click is on the sprite. if (!dragging && mouseEvent.Pressed) { dragging = true; } } // Stop dragging if the button is released. if (dragging && !mouseEvent.Pressed) { dragging = false; } } else { if (inputEvent is InputEventMouseMotion motionEvent && dragging) { // While dragging, move the sprite with the mouse. sprite.Position = motionEvent.Position; } } }}

    Touch events

    InputEventScreenTouch is equivalent to a mouse click event, and InputEventScreenDrag works much the same as mouse motion. Tip To test your touch events on a non-touchscreen device, open Project Settings and go to the “Input Devices/Pointing” section. Enable “Emulate Touch From Mouse” and your project will interpret mouse clicks and motion as touch events.