Is there a way to get the button hold time from Enhanced Input?

Enhanced Input offers the possibility to trigger key presses after holding them down for some time, but is there a way to get the elapsed/remaining hold time?

If I want to show the hold-down indication, I still have to create several variables and use tick to count the hold time, but I feel like there should be a way to extract this value from the Enhanced Input system. Is there?

Thank you!

You can try using the Quartz clock subsystem

1 Like

Okay, there is an “elapsed time” variable, but it’s inside the FInputActionInstance struct. There is a constructor that allows creating an FInputActionInstance from UInputAction

FInputActionInstance(const UInputAction* InSourceAction);

but when I try doing that, the elapsed time value is always 0.

What is the correct way to create an input instance in c++ to get hold of these values, like it’s done in this blueprint node?

image

1 Like

Ok go inside your input action and under Triggers add “Hold”.
You will get elapsed seconds added to your input event :slight_smile:

If you print out Elapsed seconds from the Ongoing exec pin you get the elapsed time shown on screen.

To do it in c++ I’d guess inside of SetupPlayerInputComponent


UInputTriggerHold* HoldTrigger = NewObject<UInputTriggerHold>();		
		YourAction->Triggers.Add(HoldTrigger);

where YourAction is a pointer UInputAction

After pressing play I can confirm it does add hold to the input action button triggers via code.

Edit:

Ok so to get the elapsed time in c++ I tried an example with jump where AtppCharacter is my character class

the bind inside SetupPlayerInputComponent

		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Ongoing, this, &AtppCharacter::IsJumping);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);

the function to display the elapsed time

void AtppCharacter::IsJumping(const FInputActionInstance& Instance) {
		
	GEngine->AddOnScreenDebugMessage(1, 1, FColor::Cyan, "IS JUMPING " + FString::SanitizeFloat(Instance.GetElapsedTime()));
}

function def in header

UFUNCTION()
	void IsJumping(const FInputActionInstance& Instance);
8 Likes

Yes, this works, thank you.
I didn’t know you could pass the instance as a function parameter here.

I can’t see where I can mark this as resolved =(

1 Like

Can’t help you there. Never made a question myself, I just try to answer them :wink:

Maybe the mods will pick this thread up with time and mark it as resolved.

1 Like