Send Special Keys Over Remote Desktop

≡放荡痞女 提交于 2019-12-10 12:01:11

问题


I am able to send special keys on local machine but the same thing is not working on remote machine. I referred to many articles but could not find the code to send special keys to remote desktop connection. Please help on this. Below is the code.

 static void Main(string[] args)
    {
        Thread.Sleep(3000);
        //char[] keyboardStrokes = { (char)Keys.LWin, (char)Keys.R };
        char[] keyboardStrokes = { (char)Keys.LMenu, (char)Keys.F4 };
        SendData(keyboardStrokes);

    }
    struct INPUT
    {
        public INPUTType type;
        public INPUTUnion Event;
    }

    [StructLayout(LayoutKind.Explicit)]
    struct INPUTUnion
    {
        [FieldOffset(0)]
        internal MOUSEINPUT mi;
        [FieldOffset(0)]
        internal KEYBDINPUT ki;
        [FieldOffset(0)]
        internal HARDWAREINPUT hi;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct KEYBDINPUT
    {
        public short wVk;
        public short wScan;
        public KEYEVENTF dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct HARDWAREINPUT
    {
        public int uMsg;
        public short wParamL;
        public short wParamH;
    }

    enum INPUTType : uint
    {
        INPUT_KEYBOARD = 1
    }

    [Flags]
    enum KEYEVENTF : uint
    {
        EXTENDEDKEY = 0x0001,
        KEYUP = 0x0002,
        SCANCODE = 0x0008,
        UNICODE = 0x0004
    }

    [DllImport("user32.dll", SetLastError = true)]
    static extern UInt32 SendInput(int numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);

    [DllImport("user32.dll")]
    static extern IntPtr GetMessageExtraInfo();

    private static void SendData(char[] c)
    {
        ProcessKey(c);
    }


    private static void ProcessKey(char[] key)
    {
        // create input events as unicode with first down, then up
        INPUT[] inputs = new INPUT[key.Length + 1];
        for (int i = 0; i < key.Length; i++)
        {
            inputs[i].type = INPUTType.INPUT_KEYBOARD;
            inputs[i].Event.ki.dwFlags = KEYEVENTF.UNICODE;
            inputs[i].Event.ki.wScan = (short)key[i];
            inputs[i].Event.ki.wVk = (short)key[i];
        }
        //Thread.Sleep(3000);
        inputs[key.Length].type = INPUTType.INPUT_KEYBOARD;
        inputs[key.Length].Event.ki.dwFlags = KEYEVENTF.KEYUP;
        inputs[key.Length].Event.ki.dwExtraInfo = GetMessageExtraInfo();
        // inputs[key.Length].Event.ki.wScan =
        // inputs[key.Length].Event.ki.dwFlags |= KEYEVENTF.KEYUP;
        uint cSuccess = SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
        if (cSuccess != inputs.Length)
        {
            throw new Win32Exception();
        }
    }

Thanks in advance.


回答1:


Finally I am able to send most of the special keys to remote machine by using following code. The only issue is that special keys remain pressed after the operation is completed. please let me know how to release the special keys.

class Program
{
    static void Main(string[] args)
    {
        Thread.Sleep(3000);
        int[] keyboardStrokes = { (int)Keys.LMenu, (int)Keys.Tab, (int)Keys.Tab };
        ProcessKey(keyboardStrokes);
        Console.Read();
    }
    struct INPUT
    {
        public INPUTType type;
        public INPUTUnion Event;
    }

    [StructLayout(LayoutKind.Explicit)]
    struct INPUTUnion
    {
        [FieldOffset(0)]
        internal MOUSEINPUT mi;
        [FieldOffset(0)]
        internal KEYBDINPUT ki;
        [FieldOffset(0)]
        internal HARDWAREINPUT hi;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct KEYBDINPUT
    {
        public ushort wVk;
        public ushort wScan;
        public KEYEVENTF dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct HARDWAREINPUT
    {
        public int uMsg;
        public short wParamL;
        public short wParamH;
    }

    enum INPUTType : uint
    {
        INPUT_KEYBOARD = 1
    }

    [Flags]
    enum KEYEVENTF : uint
    {
        EXTENDEDKEY = 0x0001,
        KEYUP = 0x0002,
        SCANCODE = 0x0008,
        UNICODE = 0x0004
    }

    [DllImport("user32.dll", SetLastError = true)]
    static extern UInt32 SendInput(int numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    internal static extern uint MapVirtualKey(uint uCode, uint uMapType);

    private static void ProcessKey(int[] key)
    {
        INPUT[] inputs = new INPUT[key.Length + 1];
        for (int i = 0; i < key.Length; i++)
        {
            uint skey = MapVirtualKey((uint)key[i], (uint)0x0);
            inputs[i].type = INPUTType.INPUT_KEYBOARD;
            inputs[i].Event.ki.dwFlags = KEYEVENTF.SCANCODE;
            inputs[i].Event.ki.wScan = (ushort)skey;
        }
        inputs[key.Length].type = INPUTType.INPUT_KEYBOARD;
        inputs[key.Length].Event.ki.dwFlags = KEYEVENTF.UNICODE;
        inputs[key.Length].Event.ki.dwFlags |= KEYEVENTF.KEYUP;
        uint cSuccess = SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
    }

}


来源:https://stackoverflow.com/questions/27187004/send-special-keys-over-remote-desktop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!