New unity input system - is there a way to not execute an action if a modifier is being pressed?

Viewed 1246

So I want 2 different things to happen for when SHIFT+Q is pressed and when merely 'q' is pressed.

Currently when I press SHIFT+Q both actions are being fired, and I don't want that. Is there a way to do this elegantly in the action mapping? I would prefer not to have to hardcode keys in my scripts, but if that's the only way then I will just check in script if shift is currently engaged. It will be a giant headache in the future though if I want to allow players to edit key mappings.

2 Answers

Edit: please scroll to the bottom of this post. It turns out there’s actually a way to do this natively

I’m not totally sure, but one thing that I know will for sure work is to have separate actions mapped to shift and to q. Then in your OnShift function, simply set a “shiftIsPressed” flag to true or false, depending on whether the context is starting or ending. Then your your OnQ function can look something like this:

void OnQ(CallbackContext ctx)
{
    if(!ctx.actionStarted) return;
    if(shiftPressed) ShiftQ();
    else Q();
}

This will keep key remapping open to you. It’s not perfect, if the player remaps Q to, say R, it will also remap ShiftQ to ShiftR. Without knowing more about your application though, that sounds like a good thing.

Edit:

To get Shift+Q to register as a separate action from Q, all you need to do is click the plus button on your ShiftQ action and select “Button With One Modifier Composite”. Then set Shift as the modifier and Q as the button. If you want to rebind Shift+Q to, say R, then set the button to R and the modifier to “Any Button”.

Hope this helps!

For anyone coming across this in the future - It looks like this feature is being implemented in InputSystem 1.4.0+

(ChangeLog: https://github.com/Unity-Technologies/InputSystem/blob/develop/Packages/com.unity.inputsystem/CHANGELOG.md)

From the changelog:

Added support for keyboard shortcuts and mutually exclusive use of modifiers. In short, this means that a "Shift+B" binding can now prevent a "B" binding from triggering.

Just need to pick one of the new 'OneModifierComposite' or 'TwoModifierComposite'.

Related