C# Creating the Simplest way of Assigning Custom Inputs At Runtime

I’m looking to create the simplest way of assigning custom inputs at runtime. My first idea for this was to declare a keycode variable, assign it via a string and call it with getkey. Except you can’t assign a string with to a keycode. Does Unity have a function similar To ToString() except with keyCodes? If it doesn’t I have some pseudocode of how it might work. I could use some help fleshing the pseudocode out as I don’t think that’s how ToString() functions.

Public Keycode myKeyCode;
    void Update(){
        if (Input.GetKey(myKeyCode)){
            //Execute Something
        }
    }
Public [] KeyCode keyCodes;
public [] String keyCodeStrings;
void ToKeyCode(KeyCode k, String s,){
    for (int i = 0; i < keyCodeStrings.Length; i++){
        if(s = keyCodeStrings[i]){
            k = keyCodes[i];
        }
    }
}

Hey DangerousBeans!

You can use this instead:

void Update() {
    if(Input.GetKeyDown(YourString)) {

    }
}

Or if you want key down/up in 1 you can use Input.GetKey instead.

Hope this helps!

ToString will work on everything, as its a member of System.Object. Keycodes in particular are an enum. You can also switch a string back to an enum with Enum.Parse.