Input.GetKey() with multiple enum values (C#)

Declaring

private KeyCode ForwardKey = KeyCode.W|KeyCode.UpArrow;

and calling
if (Input.GetKey( ForwardKey ))
{
print(“forwardPressed”)
}
during Update isn’t working out

Now I want to make a function that splits up the enum into separate values again so I can call getKey for every keycode given.

Sorry for the lack of comments, but here’s what I came up with.

The StackedKeyCodesAsInt generates a valid int that is unique for every keycode combination imaginable, the modifier of 429 is the highest number in the KeyCode enum.

The IntAsExpandedKeyCodes is the backwards conversion, the other functions are wrappers to replace the Input functions. And there’s that.

public static class KeyCodeManager
{
    public static bool GetKeys( int keycodestack )
    {
        foreach (KeyCode key in IntAsExpandedKeyCodes( keycodestack ))
            if (Input.GetKey( key ))
                return true;
        return false;
    }

    public static bool GetKeysDown( int keycodestack )
    {
        foreach (KeyCode key in IntAsExpandedKeyCodes( keycodestack ))
            if (Input.GetKeyDown( key ))
                return true;
        return false;
    }

    public static bool GetKeysUp( int keycodestack )
    {
        foreach (KeyCode key in IntAsExpandedKeyCodes( keycodestack ))
            if (Input.GetKeyUp( key ))
                return true;
        return false;
    }

    public static int StackedKeyCodesAsInt( KeyCode[] keycodes )
    {
        int output = 0;
        int modifier = 1;
        foreach (KeyCode keycode in keycodes)
        {
            output += modifier * (int)keycode;
            modifier *= 429;
        }
        return output;
    }

    public static KeyCode[] IntAsExpandedKeyCodes( int keycodestack )
    {
        List<KeyCode> output = new List<KeyCode>() { (KeyCode)(keycodestack % 429) };
        while (keycodestack > 429)
        {
            keycodestack /= 429;
            output.Add( (KeyCode)(keycodestack % 429) );
        }
        return output.ToArray();
    }
}

Like i said in the comment the KeyCode enum is not a bit mask and therefore the members can’t be combined bitwise.

You did this:

KeyCode.UpArrow == 273 == 100010001
KeyCode.W       == 119 ==   1110111
-----------------------------------
ForwardKey      ==        101110111 == 375 == KeyCode.Joystick2Button5

You have to test your keys seperately. If you want to assign multiple KeyCodes to a certain action, use an array:

private KeyCode[] ForwardKeys = new KeyCode[] {KeyCode.W, KeyCode.UpArrow};

public static bool AnyKey(KeyCode[] aKeys)
{
    foreach(KeyCode K in aKeys)
        if (Input.GetKey(K))
            return true;
    return false;
}

// Somewhere else
if (AnyKey(ForwardKeys))
{
    //
}

Now you can assign as many KeyCodes as you want.

With Unity’s built in Input Manager you can assign a key and an alternate key for each action known as ‘axes’ which can be called using a string name. You can also add your own custom actions to the input manager.

Input.GetButton("Fire1")

See http://docs.unity3d.com/Documentation/Manual/Input.html

and http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButton.html

Here i think thisll help I used it myself but here is the C# version of course

if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.UpArrow))

im not sure about the uparrow just mess around with it as all