androidunity-game-engineinputkeycodevuzix

How to map an Android keycode to a Unity keycode?


I am trying to get the touchpad of the Vizux M400 to work in Unity. The Vuzix runs on Android and I know that for example a touchpad swipe forward is handled with the android keycode KEYCODE_DPAD_RIGHT (22).

How do I map this keycode now to a keycode in Unity, so that I have access to it? I heard that I might need to create a plugin for that, but I have no idea where to start creating such a plugin. (Side info: Tapping on the touchscreen is received as Mouse0 in Unity, but tapping with 2 fingers is not recognized. So I guess these are not mapped on default)

Any help is appreciated, thank you already!


Solution

  • I don't have the device, so it is difficult for me to test. Generally, you can check the KeyCode of any recognizable device using the following.

    // Put this in Update OR OnGUI
    if (Input.anyKeyDown)
    {
        Event e = Event.current;
        if (e.isKey)
        {
            Debug.Log(e.keycode.ToString())
        }
    }
    

    After finding the keycode, use the following code for checking the state:

    KeyCode KEYCODE_DPAD_RIGHT = (KeyCode)(<insert your found keycode>)
    if (SystemInfo.deviceModel.ToLower().Contains("vuzix"))
    {
        if (Input.GetKeyDown(KEYCODE_DPAD_RIGHT))
        {
            // Do anything
        }
    }
    

    Edit 1

    I believe you can get this to work by explicitly telling Unity to check the different KeyCodes:

            for (int i = 0; i < 1000; i++)
            {
                try
                {
                    if (Input.GetKeyDown((KeyCode)i))
                    {
                        j++;
                        dText.text = j +" with: "+ i.ToString();
                        Debug.Log("Working");
                        break;
                    }
                }
                catch
                {
                }
            }
    

    Edit 2

    Run this code in Update and spam clicking your buttons and swiping/touching actions. You may get a prompt that shows that the action is recognized, and you may confirm that the actions are actually mapped by default to some of the keycodes.