Esempio n. 1
0
        private static NativeMethods.INPUT CreateInput(ushort virtualKeyCode, bool keyDown)
        {
            NativeMethods.INPUT input = new NativeMethods.INPUT();
            input.type = NativeMethods.INPUT_KEYBOARD;
            input.mkhi.ki.wVk = virtualKeyCode;
            input.mkhi.ki.time = 0;
            input.mkhi.ki.wScan = 0;
            input.mkhi.ki.dwExtraInfo = IntPtr.Zero;
            input.mkhi.ki.dwFlags = NativeMethods.KEY_SCANCODE;

            if (virtualKeyCode == 0x0D ||
                (virtualKeyCode >= 0x21 && virtualKeyCode <= 0x28) ||
                virtualKeyCode == 0x2D ||
                virtualKeyCode == 0x2E ||
                (virtualKeyCode >= 0x5B && virtualKeyCode <= 0x5D) ||
                virtualKeyCode == 0x6F ||
                virtualKeyCode == 0x90 ||
                virtualKeyCode == 0xA3 ||
                virtualKeyCode == 0xA5)
            {
                input.mkhi.ki.dwFlags |= NativeMethods.KEY_EXTENDED;
            }

            uint scanCode = NativeMethods.MapVirtualKeyEx(virtualKeyCode, 0, _hkl);
            if (virtualKeyCode == 0x13)
            {
                scanCode = 0x04C5;
            }

            if (keyDown)
            {
                input.mkhi.ki.wScan = (ushort)(scanCode & 0xFF);
            }
            else
            {
                input.mkhi.ki.wScan = (ushort)scanCode;
                input.mkhi.ki.dwFlags |= NativeMethods.KEY_UP;
            }

            return input;
        }