How to send an input key event from code?

I’m creating a plugin that accepts input from over a network.

I’m trying to send these key events to unreal as if those keys had been pressed on the keyboard of the local machine.

However, I can’t find any way to post input events.

I looked inside InputCore, InputDevice, InputCoreTypes, pretty much any module I could find with input in the name, but no luck.

Hi Wizardoutlaw,

Have you set up your Action Mappings in the Editor?

Found a solution myself.

I grabbed the viewport client through

GEngine->GameViewport->Viewport->GetClient()

And then sent the input through the client’s input method

Client->InputKey(Viewport, 0, Key, EInputEvent::IE_Pressed);
1 Like

No.

Though I did find a satisfactory solution, I am curious as to how this would fix the problem.

Hi Wizardoutlaw,

I initially misread your question and missed that you wanted to send inputs over a network. However, Action Mappings may still be useful, depending on exactly what you are trying to do. Action Mappings are used to fire input events when specific keys, or combinations of keys, are pressed. You can then specify in code what happens whenever that event occurs by referencing the name of the Action Mapping. The primary benefit of doing it like this would be that you can change the keys for the input, but your code will still work since it references the name of the Action Mapping instead of a specific key. You can see a simple Action Mapping setup here.

Hello, it can send keys combination event ?
I am stuck with this problem too.

Hello, it can send keys combination event ?
I am stuck with this problem too.

What do I put in ‘Key’ ? or how do i construct a Key? I want to simulate a mouse click.

Oh I found out:
FKey MouseLMB = EKeys::LeftMouseButton;

UGameViewportClient::InputKey() calls APlayerController::InputKey() of a ULocalPlayer

So, call

APlayerController* PC = UGameplayStatics::GetPlayerController(GetWorld(), 0);

or

APlayerController* PC = GEngine->GetFirstLocalPlayerController(GetWorld());

to get the player controller of first local player.

Then call

if (nullptr != PC)
{
    PC->InputKey(Key, EInputEvent::IE_Pressed, 1.0f, false);
}

Key is a FKey, like EKeys::W etc.

If you want to check combination keys, use

if (PC->IsInputKeyDown(SomeOtherKey))
{
    ....
}

in receiver-side fuctions.

Related question and links:
How to get the Player Controller with C++?
APlayerController::InputKey
APlayerController::IsInputKeyDown

Hey would you happen to know how to do this through blueprints?

Unfortunately, this doesn’t work for Enhanced Input. Does anyone know how to achieve the same result for the Enhanced Input?