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

I have an action for mouse LeftClick and then made a different chorded action for Shift+LeftClick, when the Shift+LeftClick action is performed in-game the LeftClick action also fires since it still detects a Left Click, how do you properly handle this with enhanced input in C++? Thank you for any help!

I don’t know if this is posible but you can try this:
Make a boolien check, if Shift key is pressed down = true, then when LeftClick run the code, if boolien = false on LeftClick, the run secound code.

That was my first thought as well, but the odd thing is the enhancedinput system doesnt appear to have a function to check for keypresses.

I know you use C++, and I’m not good with C++ myself, but I was able to do it in Blueprint, do you want an image of that Blueprint?

I appreciate it but the C++ implementation and options is completely different. Definitely very easy to do in Blueprints but I am stumped in C++ :confused:

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.

Thank you again, but most of those functions are for the older input system, they removed almost all of those functions in the enhancedinputsystem. Hopefully someone else can chime in and help. :wink:

soryy that I can’t help you then.

I hope you find out how to do it.

1 Like