Rebiding of keyboard/mouse/Joystick/Pad controls in the game

After jumping through a lot of hoops, I finally managed to write the code for control bindings… but almost…

From player’s point of view, it is standard binding procedure. Inside the options menu/controls panel, there is a button named “jump” and when player clicks on it, game is going to wait for input of a new key. And this works… but only for keyboard keys and mouse buttons. Unless I write a really bizarre piece of code, I am unable to find solution for joystick/pad buttons to be accepted as well.

I am using OnGUI for this:

void OnGUI
{
    [...]
    event e = e.current;
    if(e.isKey || e.isMouse)
    {
        [...]
        if(e.isKey)
        {
            if(e.keyCode != KeyCode.Escape)
                ControlBinds.buttonJump = e.keyCode;
            else
                ControlBinds.buttonJump = KeyCode.None;
        }
        else if(e.isMouse)
        {
            if(e.button == 0)
                ControlBinds.buttonJump = KeyCode.Mouse0;
            else if(e.button == 1)
                ControlBinds.buttonJump = KeyCode.Mouse1;
            else if(e.button == 2)
                ControlBinds.buttonJump = KeyCode.Mouse2;
        }
        [...]
    }
    [...]
}

Btw, second if and following else if are yet another ugly piece of code, but it was the only way for me to force mouse button bindings to be accepted. And that only for three standard mouse buttons. Whatever KeyCode.Mouse3/4/5/6/7/8 are, those do not recognize my mouse’s 4,5,6,7 buttons. Anyway, I am really missing here something like… e.isJoy ( if(e.isKey || e.isMouse || e.isJoy) ) and e.joyButton ( else if(e.joyButton) ), which is what is preventing me for allowing joystick button binds. It’s quite frustrating that e.keyCode sees only keyboard keys, while KeyCode enum has mouse and joystick stuff as well. Is there a sane way around this limitation, perhaps?

It seems I was searching for solution in the wrong way.

I managed to find a different, better, way for this. In case anyone interested in my solution, check this video first…

...and then check this code of mine:
Public EventSystem eventSystem;
private GameObject currentButtonControls;
private float rebindPause = 0.1f;
void Update()
{
    if(currentButtonControls != null)
    {
        eventSystem.gameObject.SetActive(false);
        currentButtonControls.GetComponent<Image>().color = new Color32(255, 0, 0, 255);
        currentButtonControls.transform.Find("Text").GetComponent<Text>().text = "Press key...";
        if(Input.anyKeyDown && rebindPause <= 0.0f)
        {
            System.Array values = System.Enum.GetValues(typeof(KeyCode));
            if(currentButtonControls.name == "Jump")
            {
                if(!Input.GetKeyDown(KeyCode.Escape))
                {
                    foreach(KeyCode key in values)
                    {
                        if(Input.GetKeyDown(key))
                            ControlBinds.buttonJump = key;
                    }
                }
                else
                    ControlBinds.buttonJump = KeyCode.None;
                currentButtonControls.transform.Find("Text").GetComponent<Text>().text = ControlBinds.buttonJump.ToString();
            }

            currentButtonControls.GetComponent<Image>().color = new Color32(255, 255, 255, 255);
            eventSystem.gameObject.SetActive(true);
            eventSystem.SetSelectedGameObject(currentButtonControls);
            rebindPause = 0.1f;

            currentButtonControls = null;
        }
        else
            rebindPause -= Time.deltaTime;
    }
}

ControlBinds is just a static class used at various places throughout the code.