First you'll need to extend your input binding map to handle things that aren't key codes, something like this:
public enum BindingType {
Unset,
Keyboard,
MouseButton,
MouseScroll
}
[System.Serializable]
public struct InputBinding {
public BindingType type;
public KeyCode key;
public int mouseButton;
public Vector2 scrollDirection;
public InputBinding(KeyCode key) {
type = BindingType.Keyboard;
this.key = key;
mouseButton = -1;
scrollDirection = Vector2.zero;
}
public InputBinding(int mouseButton) {
type = BindingType.MouseButton;
key = KeyCode.None;
this.mouseButton = mouseButton;
scrollDirection = Vector2.zero;
}
public InputBinding(Vector2 scrollDirection) {
type = BindingType.MouseScroll;
key = KeyCode.None;
mouseButton = -1;
this.scrollDirection = scrollDirection;
}
public bool Fire() {
switch(type) {
case BindingType.Keyboard:
return Input.GetKeyDown(key);
case BindingType.MouseButton:
return Input.GetMouseButtonDown(mouseButton);
case BindingType.MouseScroll:
return Vector2.Dot(Input.mouseScrollDelta, scrollDirection) > 0f;
}
Debug.LogError("Looks like you added a new input type and forgot to wire up the Fire() method.");
return false;
}
public override string ToString() {
switch(type) {
case BindingType.Keyboard:
return key.ToString();
case BindingType.MouseButton:
switch(mouseButton) {
case 0: return "Primary Click";
case 1: return "Secondary Click";
case 2: return "Middle Click";
default: return "Error - Invalid Mouse Button";
}
case BindingType.MouseScroll:
if (scrollDirection.y > 0)
return "Mouse Scroll Up";
if (scrollDirection.y < 0)
return "Mouse Scroll Down";
if (scrollDirection.x > 0)
return "Mouse Scroll Right";
if (scrollDirection.x < 0)
return "Mouse Scroll Left";
return "Error - Invalid Mouse Scroll";
}
return "Error - Invalid Input BindingType";
}
}
Dictionary<string, InputBinding> inputBindings = new Dictionary<string, InputBinding>();
You could also do this with polymorphism rather than a union, but the way shown here lets you manipulate these bindings in the Unity inspector, which doesn't handle serialization of polymorphic types as well as we might like. Note that you could compact the storage of this type by overlapping the fields, since they're mutually exclusive in use.
Then you can break out your search for an input:
InputBinding CaptureFirstInput() {
Event e = Event.current;
if (e.isKey && e.keyCode != KeyCode.None)
return new InputBinding(e.keyCode);
for (int i = 0; i < 3; i++)
if (Input.GetMouseButtonDown(i))
return new InputBinding(i);
if(Input.mouseScrollDelta != Vector2.zero)
return new InputBinding(Input.mouseScrollDelta);
return default(InputBinding);
}
And apply the return value to your input mapping:
private void OnGUI()
{
if(curKey != null)
{
var input = CaptureInput();
if(input.type != BindingType.None) {
inputBindings[curKey.name] = input;
curKey.transform.GetChild(0).GetComponent<Text>().text = input.ToString();
curKey.GetComponent<Image>().color = normal;
curKey = null;
}
}
}