# How to send an input key event from code?

**URL:** <https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270>\
**Category:** C++\
**Tags:** how-to, Input, unreal-engine, question, CPP\
**Created:** [June 19, 2014, 12:16am UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270 "2014-06-19T00:16:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_18f53fd0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_18f53fd0](https://forums.unrealengine.com/u/anonymous_user_18f53fd0)\
**Post date:** [June 19, 2014, 12:16am UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/1 "2014-06-19T00:16:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b666e59e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b666e59e](https://forums.unrealengine.com/u/anonymous_user_b666e59e)\
**Post date:** [June 19, 2014, 9:51pm UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/2 "2014-06-19T21:51:58Z")

</div>

Hi Wizardoutlaw,

Have you set up your Action Mappings in the Editor?

 ![8824-actionmappings.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/9/f499d40c7b2bce543f57c541602b3c373a1659b3.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_18f53fd0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_18f53fd0](https://forums.unrealengine.com/u/anonymous_user_18f53fd0)\
**Post date:** [June 19, 2014, 11:22pm UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/3 "2014-06-19T23:22:17Z")

</div>

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);

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_18f53fd0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_18f53fd0](https://forums.unrealengine.com/u/anonymous_user_18f53fd0)\
**Post date:** [June 20, 2014, 3:13pm UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/4 "2014-06-20T15:13:52Z")

</div>

No.

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b666e59e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b666e59e](https://forums.unrealengine.com/u/anonymous_user_b666e59e)\
**Post date:** [June 20, 2014, 3:56pm UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/5 "2014-06-20T15:56:05Z")

</div>

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](https://wiki.unrealengine.com/First_Person_Shooter_C%2B%2B_Tutorial#ActionMappings)**.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c5043162](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c5043162](https://forums.unrealengine.com/u/anonymous_user_c5043162)\
**Post date:** [November 12, 2018, 5:04am UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/6 "2018-11-12T05:04:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c5043162](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c5043162](https://forums.unrealengine.com/u/anonymous_user_c5043162)\
**Post date:** [November 12, 2018, 5:05am UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/7 "2018-11-12T05:05:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EstherZuidgeest](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EstherZuidgeest](https://forums.unrealengine.com/u/EstherZuidgeest)\
**Post date:** [April 1, 2019, 12:48pm UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/8 "2019-04-01T12:48:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EstherZuidgeest](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EstherZuidgeest](https://forums.unrealengine.com/u/EstherZuidgeest)\
**Post date:** [April 1, 2019, 12:50pm UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/9 "2019-04-01T12:50:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dev.TMG](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dev.TMG](https://forums.unrealengine.com/u/Dev.TMG)\
**Post date:** [June 21, 2021, 1:32am UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/10 "2021-06-21T01:32:03Z")

</div>

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++?](https://answers.unrealengine.com/questions/63322/how-to-get-the-player-controller-in-c.html)  
[APlayerController::InputKey](https://docs.unrealengine.com/4.26/en-US/API/Runtime/Engine/GameFramework/APlayerController/InputKey/)  
[APlayerController::IsInputKeyDown](https://docs.unrealengine.com/4.26/en-US/API/Runtime/Engine/GameFramework/APlayerController/IsInputKeyDown/)

---

<div class="post-metadata">

**Author:** ![Zarrar2802](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zarrar2802/32/710746_2.png) [@Zarrar2802](https://forums.unrealengine.com/u/Zarrar2802)\
**Post date:** [April 20, 2023, 4:16am UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/11 "2023-04-20T04:16:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pdenton](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pdenton/32/911626_2.png) [@pdenton](https://forums.unrealengine.com/u/pdenton)\
**Post date:** [March 26, 2024, 7:53pm UTC](https://forums.unrealengine.com/t/how-to-send-an-input-key-event-from-code/286270/12 "2024-03-26T19:53:03Z")

</div>

> [@Dev.TMG](#):
>
> `PC->InputKey(Key, EInputEvent::IE_Pressed, 1.0f, false);`

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