EnhancedInput how to prevent both actions firing when using same key but one with a modifier?

I understand.

Maybe this will help you:

In Unreal Engine 5, using Enhanced Input, you can handle this by setting up input mappings with conditions that differentiate between plain LeftClick and Shift+LeftClick. The idea is to make sure that when you press Shift+LeftClick, the game doesn’t trigger the action assigned to just LeftClick.

Here’s how you can set up your input to handle this situation:

  1. Define Your Input Actions: You need to have two separate actions defined in your project settings: one for LeftClick and another for Shift+LeftClick.
  2. Create Conditions for Actions:
  • For LeftClick, set a condition that it should only fire when Shift is not pressed.
  • For Shift+LeftClick, set a condition that it should only fire when Shift is pressed.
  1. Implementing the Code:
  • You’ll want to bind these actions in your character or controller class, implementing the respective C++ functions for each action.

Here’s an example of how you can set this up in your C++ code:

Step 1: Define the Input Actions

Make sure these actions are defined in your project settings under Edit -> Project Settings -> Input.

Step 2: Bind the Actions in C++

In your character or controller C++ file, bind these actions. Here’s a rough example of how it might look:

void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    // Binding the LeftClick action with a condition
    FInputActionBinding LeftClickBinding = InputComponent->BindAction("LeftClick", IE_Pressed, this, &AMyCharacter::OnLeftClick);
    LeftClickBinding.bConsumeInput = true;
    LeftClickBinding.ActionDelegate.GetDelegateForManualSet().BindLambda([this]() {
        if (!IsShiftPressed()) {
            OnLeftClick();
        }
    });

    // Binding the Shift+LeftClick action
    FInputActionBinding ShiftLeftClickBinding = InputComponent->BindAction("ShiftLeftClick", IE_Pressed, this, &AMyCharacter::OnShiftLeftClick);
    ShiftLeftClickBinding.bConsumeInput = true;
    ShiftLeftClickBinding.ActionDelegate.GetDelegateForManualSet().BindLambda([this]() {
        if (IsShiftPressed()) {
            OnShiftLeftClick();
        }
    });
}

bool AMyCharacter::IsShiftPressed() const
{
    return InputComponent->GetAxisValue("Shift") > 0;  // Assuming "Shift" is an axis or button defined in your Input Settings
}

void AMyCharacter::OnLeftClick()
{
    // Handle normal left click
}

void AMyCharacter::OnShiftLeftClick()
{
    // Handle shift left click
}

Key Points:

  • Consume Input: Note the use of bConsumeInput = true; which prevents other bindings from firing once an action is consumed. Adjust this based on whether you want other inputs to be ignored once one is handled.
  • Check Shift State: The lambda functions check the state of the shift key before deciding to execute the action, which ensures that only the appropriate handler is called.

This setup should effectively allow you to differentiate between LeftClick and Shift+LeftClick actions without them interfering with each other.