IsActionPressed returns true when key is not pressed

Godot Version

v4.7.2.stable.mono.official [ed1daf0bf]

Question

I’m checking if key is pressed like this:

    public bool WantToUseSpecialAttack()
    {
        return Godot.Input.IsActionPressed(_attackSpecial);
    }

And then I’m polling for inputs in _Process:

    public override void _Process(double delta)
    {
        //do irrelevant stuff

        if (_input.WantToUseSpecialAttack())
        {
            _weapon.UseAttack();
        }
    }

The problem - rarely, but consistently IsActionPressed will keep returning true once key is no longer pressed (key must be pressed first, it won’t return true without input), and it will keep returning true until key is pressed again.

Is there anything that I should know about? I have found some post from year ago that doesn’t exactly mention this issue, but from what I understand IsActionPressed is buggy (due to certain limitations, it’s not bad design on Engine’s side) and should not be used. But I want to be sure before I try to come up with solutions that will inevitably increase complexity of the code.

Without seeing the rest of your code, I’m going to guess that you are wrong about IsActionPressed and the problem is elsewhere. IsActionPressed has its limitations, but failing to detect when an input is released shouldn’t be one of them.

I’ve already made tests to isolate it. WantToUseSpecialAttack returns true on loop without key being pressed, and it contains nothing but IsActionPressed. I’ve also used external script that monitors shift key (action is tied to shift at the moment) to confirm that it’s not the case of key being physically stuck or some Windows glitches.

I’ve never had this happen to me before, and I’d have to agree with @rainerdeyke. While your snippets are simple, they don’t provide all the code necessary for us readers to comprehend your program; we can only guess.

That said, a few things come to mind:

  1. Have you checked that your keyboard isn’t malfunctioning?
  2. Have you tried binding the action to another key? SHIFT is a modifier key so perhaps that causes the issue?
  1. If the keybord is broken then script would catch it. But I never noticed any issues with shift button before.
  2. In fact I did not. The issue here is that for my use case shift is the perfect default keybind (it can be pressed without moving fingers from WASD and space). But I can run some tests just to be sure.

I’ve already implemented simple workaround - I’m using IsActionReleased and IsActionPressed from InputEvent to switch local booleans, and those WantTo methods just return the booleans. But I did not test it enough to confirm that it works properly now, trying to trigger this glitch is quite mind numbing.

The suspicious part - as I’ve just said I had more methods that rely on IsActionPressed. And only the one tied to shift has this behavior. Other two actions tied to mouse buttons never glitched. I should have said that earlier but I’ve realized it just now.

You don’t happen to have sticky keys enabled, do you?

I did check that yeah, it’s turned off. I would get that pop up that forces you back to desktop anyway.

I have made some tests with the new architecture and there are no problems so far. So my first theory is that IsActionPressed hates shift key.

I’m in the same boat and I spent days analyzing it and did several tests and I can confirm: the behavior ONLY affects the SHIFT key, not ALT, not CTRL, just SHIFT. Gamepad bindings are fine, it’s only IsActionPressedin combination with SHIFT. I also did try the anti-ghosting demo from Microsoft and since I own a mechanical “gamer” keyboard, it’s all fine. Sticky keys are turned off and obviously the SHIFT key works fine in other games (and other engines).

So if anyone has a solution for that it would be really great! And no, GetAsyncKeyState(VK_SHIFT) is not an option since I have the option of rebinding keys in-game, I won’t hardcode VK_SHIFT into my project.

Does the same problem occur if you use _input() and its InputEvent instead of Input.IsActionPressed()? That’s the recommended way to handle inputs.

_input() is called for every registered input, unlike _process() which is a continuous time-dependent function for game processing. (See docs)

Using _input() is not an option since using that only triggers the “run” action once and while the Shift key is pressed, the “run” state gets reset. Also I implemented it like in the docs where _physics_process() is used. I also checked some basic FPS examples in the asset library and those with a “run” or “sprint” option bound to the shift key, also have that buggy behavior that the key gets stuck.

I digged through the engine code but I’m not C++ programmer. Regarding other resource, it seems that the shift key is just treated differently as ctrl/alt and it may have to do with synthetic events, i.e. pressing one key plus the shift key and then releasing the shift key reads the “pressed” state still as true.

I could use Input.is_physical_key_pressed((Key.Shift) which is working but that would prevent me from making re-binding available. Also there are many issues on Github and this may be a related one (still open).

It is, in fact, an option but it looks like you would rather complain about the bug.

I’m not sure what you mean by the “run” state being reset. Could you elaborate?

If you want something to be called continuously while a button is being pressed, you can use a bool variable to store the state of the input. The variable can then be used inside _process() to conditionally run blocks of code.

var sprint: bool

func _process(delta):
    if sprint:
        # PERFORM SPRINTING

func _input(event):
    if event.is_action_pressed("your action"):
        sprint = true;
    if event.is_action_released("your action"):
        sprint = false;

If this doesn’t work for you, feel free to post any further questions.

Resources:

I’m sorry if it looked like I only want to complain, not my intention.

So, yes, I implemented it exactly as you wrote (thanks for your example) and this doesn’t work.
What happens:

  • You press/hold the “direction” key (W/A/S/D) and then hold the shift key to sprint
  • Sprinting is activated but stops right after 0.5 seconds or so (walking in the direction remains), although the shift key is still hold

That’s all. I played around with various approaches but nothing worked. I just implemented as various other controllers like this FPS example. So, no _input involved() but if you try it and play around with holding/releasing the shift key (bound to sprint), it gets stuck eventually and you keep running.

It really should work, check your keyboard for ghosting; press multiple keys including shift, make sure every key you hold down lights up, a good keyboard can press around 5 keys at a time

Unfortunately, it isn’t a ghosting issue, as I said before. I hold only two keys (W plus SHIFT) and it already happens, no multi-keys involved. I already tested that and obviously I play first-person games and can sprint successfully in those games. Also, the issue ONLY persists with the SHIFT key, CTRL and ALT (or any other key as sprint key) are working totally fine!

Hmm… it really should work.

0.5 seconds is a rather odd length of time – kinda similar to the time it takes for input echoes to appear. I’m not sure what to make of that.

Well, we’re trying to solve your sprint problem here; not the one that’s present in some other guy’s repo.


Could you show us the code you’re using to implement the sprint along with the input code you’re using? It’s much harder to diagnose your system without having access to the code in question.

I made a barebones example for you.

Example

Modified version of the CharacterBody3D script template.

extends CharacterBody3D


const SPEED = 5.0
const JUMP_VELOCITY = 4.5

var sprint: bool

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("left", "right", "forward", "backward")
	var camera_basis:= get_viewport().get_camera_3d().global_basis
	var direction := (camera_basis * Vector3(input_dir.x, 0, input_dir.y)).slide(Vector3.UP).normalized()
	var move_speed = SPEED * 2.0 if sprint else SPEED
	if direction:
		velocity.x = direction.x * move_speed
		velocity.z = direction.z * move_speed
	else:
		velocity.x = move_toward(velocity.x, 0, move_speed)
		velocity.z = move_toward(velocity.z, 0, move_speed)

	move_and_slide()
	
func _input(event: InputEvent) -> void:
	if event.is_action_pressed("sprint"):
		sprint = true
	if event.is_action_released("sprint"):
		sprint = false

With the bool in _input, I would try to use is_action_just_pressed and is_action_just_released.

Thanks a lot for providing the example, I rebuilt it and adjusted my code to your _input logic again and now it seems to work although I tried the same in the afternoon and it didn’t work.
I will check it further but for now, this is the solution!

Thanks again for your commitment, appreciated.