Home > Enterprise >  How to use custom values to get keycode?
How to use custom values to get keycode?

Time:06-26

For example: I have this line of code:

private string customKey = "A";

And I would like to get Keycode.A by using customKey, andKeycode.customKeywon't work. Could someone tell me how to do this?

CodePudding user response:

As mentioned in this, you can use Enum.Parse:

    private string key = "A";
    private KeyCode customKeyCode;

    private void Start()
    {
        customKeyCode = (KeyCode) System.Enum.Parse(typeof(KeyCode), key);
    }

    private void Update()
    {
        if (Input.GetKeyDown(customKeyCode))
        {
            print(key);
        }
    }
  • Related