How to add reaction on key press to AActor derivative?

Hello!
I’m experienced C++ programmer but absolute beginner in UE4. First of all, I have UE4 4.20.3 built from source code on Linux and got UE4Editor running. I can create some object in С++ class that derived from AActor base class - it’s moving on scene and represented as sphere on it (any other object also possible).

Is there any way to add reaction on key press? Reaction should call some C++ function in my code.
My assumption is to add some another class into scene that will catch my keypress events, and event handlers will iterate over my AActor classes and call some methods in them.

Please, could you give me some hints on how to implement that? Just link or even name of base class that best suited for this purpose?

If you need to handle keystrokes, then it is better to use Pawn and classes inherited from it, for example, Character. And set the instance optionn Input -> Auto Recevie Input -> Player 0 (Details Panel) and process the input in BP or C ++. An example can be found in First Person or Third Person Template C ++

If you want to process input in a class derived from AActor, then you need to add an InputComponent
.h file


  
#include "Runtime/Engine/Classes/Components/InputComponent.h"
...
 void SetupMyPlayerInputComponent(UInputComponent* myInputComponent);
    void keyE();

.cpp file


void AMyActor1::BeginPlay()
{
    Super::BeginPlay();

    EnableInput(GetWorld()->GetFirstPlayerController());    // ? создаёт UInputComponent в this->InputComponent
    UInputComponent* myInputComp = this->InputComponent;
    if (myInputComp) {
        SetupMyPlayerInputComponent(myInputComp);
    }
}

void AMyActor1::SetupMyPlayerInputComponent(UInputComponent* myInputComponent) {
    //Super::SetupPlayerInputComponent(InputComponent);   //Another experimental thing

    myInputComponent->BindAction("mKey1", IE_Pressed, this, &AMyActor1::keyE);  

}

void AMyActor1::keyE() {
    UE_LOG(LogTemp, Warning, TEXT("keyE"))       // will write in the log when the mKey1 event is executed
}

This code accepts input from player 0. You need to add the mKey1 event in the Project Setting settings -> Input

Thank you for the reply!
Topic solved, I did everything what I wanted to.

To be honest, I found similar example code earlier but it was lacked of EnableInput(GetWorld()->GetFirstPlayerController()) call, or may be this call were placed elsewhere… Without GetFirstPlayerController call my game dynamic library caused UE4Editor to crash every time my project attempted to open without any details displayed, until I realized to delete Binaries folder and remove newly added code.

Since I have multiple objects that want to control with different keys on keyboard,


UPROPERTY(EditAnywhere) int index;

added into class and then:


FString key = FString::Printf(TEXT("mykey%d"), index);
myInputComp->BindAction(*key, IE_Pressed, this, &ATestActor::key_pressed);

Also, GetName() might me used for purpose of distinguishing multiple object action names.