TExcelTAD
Home › Godot 4 with GDScript › The Input Map & Input Events

The Input Map & Input Events

Godot gives you two ways to read input, and real games use both:

  • Polling with the Input singleton: "is jump held right now?" Use it for continuous things inside _physics_process: movement, aiming, holding a button to charge.
  • Events delivered to callbacks: "the player just pressed Escape". Use them for one-off actions, UI, text entry, and anything another node might want to consume first.

Both are built on actions from the Input Map, so your code says "jump", never KEY_SPACE.

The Input Map

  1. Open Project > Project Settings > Input Map.
  2. Add an action (jump, pause, move_left...) with the Add New Action field.
  3. Click + on the action's row and press a key, click a mouse button, or move a gamepad stick or button. Add as many events as you like: Space and gamepad A and W.
  4. Each action has a Deadzone: analog input below it counts as zero.
  5. Tick Show Built-in Actions to see the ui_* actions (ui_accept, ui_cancel, ui_left...) that the UI nodes use for keyboard and gamepad navigation. You can add events to them, but don't delete them.

The map is saved in project.godot under an [input] section. When you add a key, the dialog lets you choose between a physical key (the key's position on the keyboard: W stays the top-left movement key on an AZERTY keyboard) and the key's label. Use physical keys for movement and labels for keys named after letters, like "I for inventory".

Polling actions

Call Meaning
Input.is_action_pressed(a) held now
Input.is_action_just_pressed(a) / is_action_just_released(a) changed this frame
Input.get_action_strength(a) 0 … 1, partial for analog triggers and sticks
Input.get_axis(neg, pos) strength(pos) - strength(neg): -1 … 1
Input.get_vector(left, right, up, down) a Vector2 for 8-way or analog movement, length ≤ 1, with a circular deadzone

get_vector does two things that hand-written code often forgets: diagonal keys give length 1 (not 1.41, which would make diagonal movement faster), and the deadzone is rescaled, so a stick just past the deadzone gives a small value instead of jumping straight to it. The examples press actions in code with Input.action_press(action, strength), which is how you'd fake a stick in a test:

gdscript · try it yourself
extends SceneTree

func _init():
	for action in ["left", "right", "up", "down"]:
		InputMap.add_action(action, 0.2)    # 0.2 = deadzone for analog input
	print("deadzone of 'right': ", snappedf(InputMap.action_get_deadzone("right"), 0.01))

	# Keyboard: right + down held (full strength)
	Input.action_press("right")
	Input.action_press("down")
	var v := Input.get_vector("left", "right", "up", "down")
	print("keys right+down: ", v.snappedf(0.001), " length ", snappedf(v.length(), 0.001))
	Input.action_release("right")
	Input.action_release("down")

	# A stick pushed 60% right: strength 0.6
	Input.action_press("right", 0.6)
	print("stick 0.6 right: axis ", snappedf(Input.get_axis("left", "right"), 0.001),
		", vector ", Input.get_vector("left", "right", "up", "down").snappedf(0.001))
	Input.action_release("right")

	# A worn stick drifting 0.1 to the left: inside the deadzone
	Input.action_press("left", 0.1)
	print("drift 0.1 left: vector ", Input.get_vector("left", "right", "up", "down"))
	Input.action_release("left")
	quit()
deadzone of 'right': 0.2
keys right+down: (0.707, 0.707) length 1.0
stick 0.6 right: axis 0.6, vector (0.5, 0.0)
drift 0.1 left: vector (0.0, 0.0)

The stick at 0.6 becomes 0.5 in the vector: (0.6 − 0.2) / (1 − 0.2). That remapping is why top-down movement code should use get_vector, and multiply by speed: velocity = Input.get_vector(...) * speed.

Input events and who gets them

Every key press, mouse click, mouse motion, touch and gamepad change becomes an InputEvent object (InputEventKey, InputEventMouseButton, InputEventMouseMotion, InputEventJoypadButton, InputEventJoypadMotion, InputEventScreenTouch...). Godot passes each event down a fixed pipeline, and any step can consume it with get_viewport().set_input_as_handled():

  1. _input(event) on every node that defines it (deepest and last-added nodes first).
  2. GUI: Control nodes under the mouse or with focus (_gui_input, buttons, text fields).
  3. _shortcut_input(event), then _unhandled_key_input(event) for keys.
  4. _unhandled_input(event): gameplay input goes here, so that a click on a button or typing in a chat box doesn't also make the player shoot.
  5. Physics picking (CollisionObject2D.input_event) for clicking on objects in the world.

Below, a pause menu catches pause in _input and marks it handled, so the player never sees it. The events are sent with Input.parse_input_event(), which queues them as if they came from a device; they're delivered at the start of the next frame, which is why the event list prints first:

gdscript · try it yourself
extends Node

# frames: 3
class PauseMenu extends Node:
	var open := false
	func _input(event: InputEvent) -> void:
		if event.is_action_pressed("pause"):
			open = not open
			print("  PauseMenu._input: pause menu open = ", open, " (event handled)")
			get_viewport().set_input_as_handled()

class Player extends Node:
	func _unhandled_input(event: InputEvent) -> void:
		if event.is_action_pressed("jump"):
			print("  Player._unhandled_input: jump")
		elif event.is_action_pressed("pause"):
			print("  Player._unhandled_input: saw pause (should not happen)")
		elif event is InputEventMouseButton and event.pressed:
			print("  Player._unhandled_input: click at ", event.position, " button ", event.button_index)

func key(code: Key) -> InputEventKey:
	var ev := InputEventKey.new()
	ev.physical_keycode = code
	ev.pressed = true
	return ev

func _ready():
	InputMap.add_action("jump")
	InputMap.action_add_event("jump", key(KEY_SPACE))
	var pad := InputEventJoypadButton.new()
	pad.button_index = JOY_BUTTON_A
	InputMap.action_add_event("jump", pad)
	InputMap.add_action("pause")
	InputMap.action_add_event("pause", key(KEY_ESCAPE))
	add_child(Player.new())
	add_child(PauseMenu.new())

	for ev in InputMap.action_get_events("jump"):
		print("jump is bound to: ", ev.as_text())

	var click := InputEventMouseButton.new()
	click.button_index = MOUSE_BUTTON_LEFT
	click.pressed = true
	click.position = Vector2(20, 10)
	var pad_press := InputEventJoypadButton.new()
	pad_press.button_index = JOY_BUTTON_A
	pad_press.pressed = true
	for ev in [key(KEY_SPACE), key(KEY_ESCAPE), pad_press, click, key(KEY_Q)]:
		print("event: ", ev.as_text())
		Input.parse_input_event(ev)
jump is bound to: Space - Physical
jump is bound to: Joypad Button 0 (Bottom Action, Sony Cross, Xbox A, Nintendo B)
event: Space - Physical
event: Escape
event: Joypad Button 0 (Bottom Action, Sony Cross, Xbox A, Nintendo B)
event: Left Mouse Button
event: Q - Physical
  Player._unhandled_input: jump
  PauseMenu._input: pause menu open = true (event handled)
  Player._unhandled_input: jump
  Player._unhandled_input: click at (20.0, 10.0) button 1

event.is_action_pressed("jump") asks whether this event is a press of an action, so the keyboard Space and the gamepad A both trigger the same code. Q isn't bound to anything and nobody reacts. Use event.is_action_pressed("jump", true) to accept key repeat (echo) events too; by default echoes are ignored.

Rebinding at runtime

A controls menu is a few lines: wait for the next key in _input, then replace the action's events. Mark the event handled, or the key that was just bound also triggers its action:

gdscript · try it yourself
extends Node

# frames: 4
var listening_for := ""     # the action being rebound, or "" when not rebinding

func key(code: Key) -> InputEventKey:
	var ev := InputEventKey.new()
	ev.physical_keycode = code
	ev.pressed = true
	return ev

func describe(action: String) -> String:
	var names := []
	for ev in InputMap.action_get_events(action):
		names.append(ev.as_text())
	return action + " = " + ", ".join(names)

func _ready():
	InputMap.add_action("jump")
	InputMap.action_add_event("jump", key(KEY_SPACE))
	print(describe("jump"))
	start_rebind("jump")                 # e.g. the player clicked "Jump" in the controls menu
	Input.parse_input_event(key(KEY_W))  # ...and then pressed W

func start_rebind(action: String):
	listening_for = action
	print("Press a key for '", action, "'...")

func _input(event: InputEvent) -> void:
	if listening_for == "" or not (event is InputEventKey and event.pressed):
		return
	InputMap.action_erase_events(listening_for)
	InputMap.action_add_event(listening_for, event)
	print("rebound: ", describe(listening_for))
	listening_for = ""
	get_viewport().set_input_as_handled()   # don't also make the player jump
	Input.parse_input_event(key(KEY_SPACE))
	Input.parse_input_event(key(KEY_W))

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventKey:
		print(event.as_text(), " -> jump? ", event.is_action_pressed("jump"))
jump = Space - Physical
Press a key for 'jump'...
rebound: jump = W - Physical
Space - Physical -> jump? false
W - Physical -> jump? true

InputMap changes are not saved: on the next launch the project settings' bindings come back. Store the player's bindings in a settings file (ConfigFile can store InputEvent objects directly) and re-apply them at startup; see Saving: FileAccess, ConfigFile & JSON. Erasing all events also removes the gamepad binding in this example; a real menu replaces only the keyboard events (check ev is InputEventKey).

Test yourself

Where should gameplay code like 'shoot on click' handle events, so clicking a UI button doesn't also shoot?

  1. _input
  2. _unhandled_input
  3. _ready
  4. _process with Input.is_key_pressed

Why does Input.get_vector("left", "right", "up", "down") return (0.707, 0.707) for right + down instead of (1, 1)?

  1. It is a bug
  2. It limits the vector's length to 1, so diagonal movement isn't faster
  3. Keys have half strength
  4. The deadzone subtracts 0.3

A player rebinds jump with InputMap.action_add_event, restarts the game, and the old key is back. Why?

  1. InputMap changes only last for the current run; you must save and re-apply them
  2. The rebind needs await
  3. Rebinding only works for gamepads
  4. project.godot is read-only in exported games and Godot throws an error