键盘代码翻译

时间:2016-07-08 12:51:07

标签: c# linux windows keyboard keycode

我在C#中创建了一些从Windows到Linux的远程控制工具。

我面临的唯一问题是将密钥代码从Windows转换为Linux。是否有任何公式,表格或工具可以进行此转换?

如果有帮助,在Windows上我从KeyUp / KeyDown事件中获取密钥代码,在linux上我使用uinput来生成事件。

干杯。

1 个答案:

答案 0 :(得分:0)

好的,我找到了一个基于Dietrich Epp概念的优雅解决方案。该想法基于使用USB HID代码作为中间公共点。

我从here获得了Linux密钥映射到HID,从here获得了windows密码映射。

这是最终结果:

    public static byte[] linux_hid_keyboard = new byte[] 
    {
        255,  255,  255,  255, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
        50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
        4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
        27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
        65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
        105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
        72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
        191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
        115,114,255,255,255,121,255, 89, 93,124, 92, 94, 95,255,255,255,
        122,123, 90, 91, 85,255,255,255,255,255,255,255,111,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,179,180,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,111,255,255,255,255,255,255,255,
        29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
        150,158,159,128,136,177,178,176,142,152,173,140,255,255,255,255
    };

    public static byte[] windows_hid_keyboard = new byte[]  
    {
        255,255,255,255, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
        77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 49, 50,
        51, 52, 53, 54, 55, 56, 57, 48, 13, 27,  8,  9, 32,189,187,219,
        221,220,255,186,222,192,188,190,191, 20,112,113,114,115,116,117,
        118,119,120,121,122,123, 44,145, 19, 45, 36, 33, 46, 35, 34, 39,
        37, 40, 38,144,111,106,109,107,255, 97, 98, 99,100,101,102,103,
        104,105, 96,110,255,255,255,255,124,125,126,127,128,129,130,131,
        132,133,134,135,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
        17, 16, 18,255,255,255,255,255,255,255,255,255,255,255,255,255,
        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
    };

    public static Dictionary<int, int> GetTranslationWindowsToLinux()
    {
        Dictionary<int, int> trans = new Dictionary<int, int>();

        for (int buc = 0; buc < 256; buc++)
        {
            int lin = linux_hid_keyboard[buc];
            int win = windows_hid_keyboard[buc];

            if (lin != 255 && win != 255 && !trans.ContainsKey(win))
                trans.Add(win, lin);
        }

        return trans;
    }

    public static Dictionary<int, int> GetTranslationLinuxToWindows()
    {
        Dictionary<int, int> trans = new Dictionary<int, int>();

        for (int buc = 0; buc < 256; buc++)
        {
            int lin = linux_hid_keyboard[buc];
            int win = windows_hid_keyboard[buc];

            if (lin != 255 && win != 255 && !trans.ContainsKey(lin))
                trans.Add(lin, win);
        }

        return trans;
    }

希望它可以帮助处于相同情况的人。