Getting Input Key Time Down in C++

Hey there!

I hope you guys can help me with this. First of all sorry for my english, I’ll try to explain as good as possible.

I have a MyDoor class in my project whose father is an UsableItem class. I want my player (own coded player which have no controller), whose father is ACharacter to close the door if he holds down the “use button” more than 1.5 seconds. The problem is, I cannot access to the Get Input Key Time Down function because the target is a Player Controller and I don’t have one of those hehe (or that’s what I think).

I’ve tried some solutions but none of them worked such as:
-Casting the ACharacter to MyPlayer in order to get the controller
-Adding the generic controller class to MyDoor to get the function
-I want to try to create my own timer, but I don’t know how to get the key I press because I’m overriding the OnUsed function from UsableItem.

ACharacter → MyPlayer
UsableItem → MyDoor

Could you help me out, please? If you want, I can post screenshots of my code, but I don’t think it’s necessary.

Thank so much in advance.



try the function GetPlayerController: As world context object you can use anything that implements GetWorld(), for instance ACharacter::GetWorld(), and as index use 0. Note that this function returns NULL during actor construction so if you are calling it in a tick make sure to check for NULL. You did not specify whether this is a multiplayer game or not. If it is, be advised that obtaining the player controller like this will only work on clients and non-dedicated servers. If the code you are writing might potentially run on both client and server, call IsLocalPlayer() on the player controller.

With that answered, I’d just like to note that every player-controlled pawn always has a player controller. For me it does not really come through as to what is the player controlled pawn; however, on whichever pawn that is you can just call GetController() on the pawn and cast it to a player controller safely. Casting your pawn to a player controller as you suggested won’t work because the classes are unrelated.


Hey Univise, thank you so much for helping me out and for your quick answer!

I tried that you said and FINALLY I could get the PlayerController and in consequence, the GetInputKeyTimeDown() function. And to clarify something, my game is a non-multiplayer game. We just control one player. And yes, I realized that last thing you said about casting my player to a controller and OF COURSE, I can’t do that haha.

My problem now is that I’m overriding the OnUsed function from my UsableItem in MyDoor class and I cannot get the time I hold down my “Spacebar” key. I’m trying this:

bool AMyDoor::OnUsed(ACharacter* character)
   if (StatesEnum == EDoorStates::DS_POSHALFOPENED && GetWorld()->GetFirstPlayerController()->GetInputKeyTimeDown(FKey("Spacebar")) == 2.0)
		StatesEnum = EDoorStates::DS_CLOSED;
		return true;
                return false;

This is: if my door is halfopened and I press spacebar during 2 seconds, close the door.

But it’s not working and I don’t know why.

Again, thank you so much for your help!

Ok, solved!

My problem was that I was trying to get the time I press a key in an “OnUsed” event. And that only triggers when I press (not release) the key. My solution was to change the InputEvent to “Release” and calculate that time in the Tick(). Once I had that it was easy to create the logic in the OnUsed function.

Thank you so much for your help, Univise!

As addition to the accepted answer by Univise, that’s how you access it:


documentation of the method.: