Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to get key used for input?

Currently, I'm searching for the c++ equivalent of getting the key used for an input, like in this blueprint:

alt text

Product Version: UE 4.12
more ▼

asked Jun 18 '16 at 03:09 PM in C++ Programming

avatar image

12 3 3 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first

This sort of behavior will require a bit more work on the C++ end. There's not a universal event that's emitted when a key is pressed.

In C++ you have a couple of options. You could monitor key presses within your player controllers Tick() function, checking WasInputKeyJustPressed(). You could also create a new InputComponent, which would allow you to better monitor input. Other than that, for keys you expect to be using, BindAction() will be your friend.

What are you trying to do that binding an input within Settings > Input isn't handling?

more ▼

answered Jun 18 '16 at 06:39 PM

avatar image

396 16 8 16

avatar image Terminator810 Jun 18 '16 at 09:14 PM

I'm checking for how long the key was held down. So, holding down the interact key for one second triggers an event. I have it working flawlessly in BP, but I need to get the key so I check how long it's held for in c++ (The keybindings are changeable so I can't just hard code it in).

avatar image FrostedSentry Jun 19 '16 at 04:04 PM

You'll use GetKeysForAction on PlayerInput to figure out what key is currently associated with the action you're looking for. Find the key and maintain a timer to replicate the behavior in C++.

avatar image FrostedSentry Jun 20 '16 at 05:23 PM

How's the adventure going? Get things worked out?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

This behavior can be achieved by just adding an "FKey" parameter to the bound method.

Create an Action input mapping on the ProjectSettings, then add all the key binds you need (AnyKey if you want all keys). Then use it like this on your code:


 virtual void SetupInputComponent() override;
 void TryAction(FKey key);


 void AMyPlayerController::SetupInputComponent() {
     InputComponent->BindAction("MyAction", EInputEvent::IE_Pressed, this, &AMyPlayerController::TryAction);
 void AMyPlayerController::TryMorph(FKey key)
     FName keyName = key.GetFName();
more ▼

answered Jul 19 '18 at 04:05 AM

avatar image

46 1 3 7

avatar image kenmorechalfant May 19 '19 at 03:19 AM

This was exactly what I was hoping for. Also FKey has a function IsGamepadKey(), which is the reason I needed it - to alter how the character works depending on whether it's keyboard or gamepad (input is hold vs toggle).

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

You can use UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetInputKeyTimeDown(FKey("AnyKey"))

 float howLongTheKeyIsPressed = UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetInputKeyTimeDown(FKey("W"));
      // This is for the jump function.
     if (bPressedJump)
         //GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("How long was the key pressed: %f"),  howLongTheKeyIsPressed ));
                // If longer than few seconds then do this, else that
         if (howLongTheKeyIsPressed*10.f > 5.f)
             // do
more ▼

answered May 20 '19 at 07:45 PM

avatar image

46 3 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question