# how can i write an custom Input Action in unreal c++

**URL:** <https://forums.unrealengine.com/t/how-can-i-write-an-custom-input-action-in-unreal-c/924839>\
**Category:** C++\
**Tags:** unreal-engine, CPP, question\
**Created:** [April 13, 2023, 5:36pm UTC](https://forums.unrealengine.com/t/how-can-i-write-an-custom-input-action-in-unreal-c/924839 "2023-04-13T17:36:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emir143](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@emir143](https://forums.unrealengine.com/u/emir143)\
**Post date:** [April 13, 2023, 5:36pm UTC](https://forums.unrealengine.com/t/how-can-i-write-an-custom-input-action-in-unreal-c/924839/1 "2023-04-13T17:36:08Z")

</div>

so i have made a custom input action with the key I, and i wanna know how i can write this BP code in c++

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/0/e/70e3a65912d28c133775f88f065f3720a78e9c79.png)

---

<div class="post-metadata">

**Author:** ![ScrewdriverHyena](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/screwdriverhyena/32/679006_2.png) [@ScrewdriverHyena](https://forums.unrealengine.com/u/ScrewdriverHyena)\
**Post date:** [April 13, 2023, 6:06pm UTC](https://forums.unrealengine.com/t/how-can-i-write-an-custom-input-action-in-unreal-c/924839/2 "2023-04-13T18:06:47Z")

</div>

This can definitely be done more elegantly, but I would defer to how Lyra handles this if you want a better solution. This should work however (keep in mind that you need to set the input action in the actor’s properties):

.h

```auto
UCLASS()
class MYGAME_API AMyActor : public AActor
{
    GENERATED_BODY()

    public:

    virtual void SetupPlayerInputComponent(UInputComponent* InputComponent);

    void OnToggleInventory();

    UPROPERTY(EditAnywhere)
    UInputAction* ToggleInventory;
}

```

.cpp

```auto
void AMyActor::SetupPlayerInputComponent(UInputComponent* InputComponent)
{
    BindAction(ToggleInventory, ETriggerEvent::Triggered, this, &AMyActor::ToggleInventory);
}

void AMyActor::ToggleInventory()
{
    // Do things here
}

```

Let me know if I missed anything, since I haven’t tested this.

---

<div class="post-metadata">

**Author:** ![emir143](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@emir143](https://forums.unrealengine.com/u/emir143)\
**Post date:** [April 13, 2023, 6:15pm UTC](https://forums.unrealengine.com/t/how-can-i-write-an-custom-input-action-in-unreal-c/924839/3 "2023-04-13T18:15:36Z")

</div>

i get error on the UInputAction variable and the  
BindAction(ToggleInventory, ETriggerEvent::Triggered, this, &AMyActor::ToggleInventory);

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [April 13, 2023, 7:13pm UTC](https://forums.unrealengine.com/t/how-can-i-write-an-custom-input-action-in-unreal-c/924839/4 "2023-04-13T19:13:19Z")

</div>

```auto
FInputActionBinding CurBinding& = InputComponent->BindAction(TEXT("OpenMenu"), EInputEvent::IE_Released, this, &APlayerControllerGame::ActOnOpenMenu);
CurBinding.bConsumeInput = true;
CurBinding.bExecuteWhenPaused = true;

```

Where “this” in the example == APlayerController and the implemented method on it ActOnOpenMenu is marked UFUNCTION. Added example of how to set some parameters on the binding but they have defaults . PlayerControllers and Pawns are the common classes to use InputComponents on. If this would be implemented on a UserWidget, forget about InputComponents because they have their own input routing system:

> [Widget input: It SMELLS](https://forums.unrealengine.com/t/widget-input-it-smells/784112)
