Home > Enterprise >  How to properly handle multiplie key presses in unity?
How to properly handle multiplie key presses in unity?

Time:08-12

I'm making shortcuts for a little project i'm doing with unity. In my game i defined two hotkeys :

1 - Shift Ctrl G

2 - Shift G

So my code looks like this:

        if (Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.G))
        {
            DoSomethingA()
        }

        if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.G))
        {
            DoSomethingB()
        }

Now when i hold control and shift and press G, DoSomethingB executes too, I thought unity has something like Input.GetKeyCount() to check if 2 keys are pressed but seems like unity missing this feature, how can I deal with this situation?

EDIT 1 : I use this function for easing the process.

[Serializable] public struct MultiHotkey2
{
    public KeyCode firstKey;
    public KeyCode secondKey;
}
[Serializable] public struct MultiHotkey3
{
    public KeyCode firstKey;
    public KeyCode secondKey;
    public KeyCode thirdkey;
}

    private bool CheckDoubleHotKey(MultiHotkey2 hotkey2)
    {
        return Input.GetKey(hotkey2.firstKey) && Input.GetKeyDown(hotkey2.secondKey) && GetGlobalUndoRedoSystemState();
    }
    private bool CheckTripleHotKey(MultiHotkey3 hotkey3)
    {
        return Input.GetKey(hotkey3.firstKey) && Input.GetKey(hotkey3.secondKey) && Input.GetKeyDown(hotkey3.thirdkey) && GetGlobalUndoRedoSystemState();
    }

CodePudding user response:

I'd recommend you to use Unity's Input System Package. It makes easier to handle multiple key inputs.

Here it is: https://docs.unity3d.com/Packages/[email protected]/manual/index.html

CodePudding user response:

For this specific example you may only check if Control Key is not pressed in second if statement, making it look like this:

    if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.G) && !Input.GetKey(LeftControl))
    {
        DoSomethingB();
    }

Also 'else if' statement would fix it. Solution:

    if (Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.G))
    {
        DoSomethingA();
    }
    else if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.G))
    {
        DoSomethingB();
    }

Program will skip second 'if statement' if the first requirements are met.

You also can edit the solution a little to make it more optimal (less Input checking):

if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.G))
{
    if (Input.GetKey(KeyCode.LeftControl))
    {
        DoSomethingA();
    }
    else
    {
        DoSomethingB();
    }
}

EDIT 1:

If you have created all your MultiHotkey2, ~3 objects the check looks like this.

if (CheckTripleHotKey(_multiHotKey3)
{
    DoSomethingA();
}
else if (CheckDoubleHotKey(_multiHotKey2)
{
    DoSomethingB();
}

Need more details on what you have for a more specific answer. If you have a lot of multihotkey objects, you probably want to iterate through them instead of checking one by one.

  • Related