Input.IsActionPressed() not working as intended on project run?

Godot Version

.Net 4.5.1

Question

Hello, I am pretty new to godot and gamedev, when I run my game I have some debugging to display if some Input.IsActionPressed() and they all have the value true even when all the button pressed are false until I leave the window focus (click outside of it) and come back to it, why is that?
Output: Actions - left:True, right:True, up:True, down:True
Keys - Left:False, Right:False, Up:False, Down:False, A:False, D:False, W:False, S:False
Input vector: (0, 0)
Until i click outside the window which then works correctly
Actions - left:False, right:False, up:False, down:False
Keys - Left:False, Right:False, Up:False, Down:False, A:False, D:False, W:False, S:False

The keys booleans always reflect the ones i am pressing, the actions all start being true until I focus another window and refocus the game one, then they correctly reflect the actions being triggered

using Godot;
using System;

public partial class Player : CharacterBody3D
{
	[Export]
	public int Speed { get; set; } = 400;

	public override void _Ready()
{
}

	public void GetInput()
	{
		var inputVec = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
		GD.Print($"Input vector: {inputVec}");
		Velocity = new Vector3(inputVec.X, 0f, inputVec.Y);

	}

	public override void _Process(double delta)
	{

		GD.Print($"Actions - left:{Input.IsActionPressed("ui_left")}, right:{Input.IsActionPressed("ui_right")}, up:{Input.IsActionPressed("ui_up")}, down:{Input.IsActionPressed("ui_down")} ");
		GD.Print($"Keys - Left:{Input.IsKeyPressed(Key.Left)}, Right:{Input.IsKeyPressed(Key.Right)}, Up:{Input.IsKeyPressed(Key.Up)}, Down:{Input.IsKeyPressed(Key.Down)}, A:{Input.IsKeyPressed(Key.A)}, D:{Input.IsKeyPressed(Key.D)}, W:{Input.IsKeyPressed(Key.W)}, S:{Input.IsKeyPressed(Key.S)}");
		GetInput();
		MoveAndSlide();
	}

}

I tried creating a new project but still get the same issue with different code. When doing the same thing in gdscript it works perfectly

using System;

public partial class Way8 : CharacterBody2D
{
	[Export]
	private int speed = 400;
	public void GetInput()
	{
		Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
		Velocity = direction * speed;
	}

	public override void _PhysicsProcess(double delta)
	{
		GetInput();
		MoveAndSlide();
	}
}

The function is_pressed() is not guaranteed to work correctly every frame due to keyboard ghosting.

Note: Due to keyboard ghosting, is_pressed() may return false even if one of the action’s keys is pressed. See Input examples in the documentation for more information.

So clicking in and out of the window may be spurious data. It could be that clicking inside could also trigger it. Also, it working in GDScript isn’t a sign that the two are different necessarily, as the functionality isn’t guaranteed for either implementation.

Still, if you want to do it, you want to use Input.is_action_just_pressed() and/or Input.is_action_just_released() and track the actions you want to watch. Otherwise you need to add code to deal with echoes which is a lot more complex.

Doing this will reduce the amount of extraneous crap in your logs and make them semi-readable. Right now you’re trying to print ~60 times per second the state of all the keys. This is an unreadable flood of information that will clog your logs and make it hard to find any other debugging information. Even just logging each keypress once when it happens will quickly become more trouble than it’s worth. But that’s how you do it.

P.S. Best practice is to create your own actions and not use the default UI actions for your character controller.

1 Like