Home > Back-end >  How to map an Android keycode to a Unity keycode?
How to map an Android keycode to a Unity keycode?

Time:10-27

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!

CodePudding user response:

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  )
    if (Input.GetKeyDown((KeyCode)i)))
    {
        Debug.Log("Working");
        break;
    }
}

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.

CodePudding user response:

The solution simply was to deactivate the mouse feature of the Vuzix M400s touchpad. It can be done in the devices settings.

So, these are the KeyCodes for the touchpad:

    private const KeyCode _oneFingerTapKeyCode = (KeyCode)330;
    private const KeyCode _oneFingerHoldKeyCode = (KeyCode)319;
    private const KeyCode _oneFingerSwipeBackKeyCode = (KeyCode)276;
    private const KeyCode _oneFingerSwipeForwardKeyCode = (KeyCode)275;
    private const KeyCode _oneFingerSwipeUpKeyCode = (KeyCode)273;
    private const KeyCode _oneFingerSwipeDownKeyCode = (KeyCode)274;
    private const KeyCode _twoFingerTapKeyCode = (KeyCode)27;
    private const KeyCode _twoFingerHoldKeyCode = (KeyCode)278;
    private const KeyCode _twoFingerSwipeForwardKeyCode = (KeyCode)127;
    private const KeyCode _twoFingerSwipeBackKeyCode = (KeyCode)8;

I have seen them somewhere in a different thread already, but I thought that they did not work, but it was just the mouse input.

  • Related