Subversion Repositories group.electronics

Rev

Rev 176 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace nitdcscore {
    public class OSInput {
        INPUT[] inputs;
        private bool control = false;
        private VirtualKeyCodes key;

        public OSInput(VirtualKeyCodes key, bool control = false) {
            this.key = key;
            this.control = control;
        }

        public void Send() {
            if (this.control) {
                this.inputs = new INPUT[2];
                inputs[0].type = INPUT_KEYBOARD;
                inputs[0].InputUnion.ki.time = 0;
                inputs[0].InputUnion.ki.wVK = 0;
                inputs[0].InputUnion.ki.dwFlags = KEYEVENTF_SCANCODE;
                inputs[0].InputUnion.ki.wScan = (ushort)MapVirtualKey((uint)VirtualKeyCodes.VK_CONTROL, 0);
                inputs[0].InputUnion.ki.dwExtraInfo = GetMessageExtraInfo();

                inputs[1].type = INPUT_KEYBOARD;
                inputs[1].InputUnion.ki.time = 0;
                inputs[1].InputUnion.ki.wVK = 0;
                inputs[1].InputUnion.ki.dwFlags = KEYEVENTF_SCANCODE;
                inputs[1].InputUnion.ki.wScan = (ushort)MapVirtualKey((uint)key, 0);
                inputs[1].InputUnion.ki.dwExtraInfo = GetMessageExtraInfo();

            } else {
                this.inputs = new INPUT[1];
                inputs[0].type = INPUT_KEYBOARD;
                inputs[0].InputUnion.ki.time = 0;
                inputs[0].InputUnion.ki.wVK = 0;
                inputs[0].InputUnion.ki.dwFlags = KEYEVENTF_SCANCODE;
                inputs[0].InputUnion.ki.wScan = (ushort)MapVirtualKey((uint)key, 0);
                inputs[0].InputUnion.ki.dwExtraInfo = GetMessageExtraInfo();
            }


            SendInput((uint)inputs.Count(), inputs, Marshal.SizeOf(typeof(INPUT)));
            System.Threading.Thread.Sleep(5);

            for (var i = 0; i < inputs.Count(); i++) {
                inputs[i].InputUnion.ki.dwFlags |= KEYEVENTF_KEYUP;
            }
            Array.Reverse(inputs);
            SendInput((uint)inputs.Count(), inputs, Marshal.SizeOf(typeof(INPUT)));
        }

        public const int INPUT_MOUSE = 0;
        public const int INPUT_KEYBOARD = 1;
        public const int INPUT_HARDWARE = 3;
        public const int KEYEVENTF_KEYUP = 0x0002;
        public const int KEYEVENTF_SCANCODE = 0x0008;

        [StructLayout(LayoutKind.Sequential)]
        public struct INPUT {
            internal uint type;
            internal InputUnion InputUnion;
            internal static int Size {
                get { return Marshal.SizeOf(typeof(INPUT)); }
            }
        }

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

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

        [StructLayout(LayoutKind.Sequential)]
        public struct HARDWAREINPUT {
            private uint uMsg;
            private ushort wParamL;
            private ushort wParamH;
        }

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

        [DllImport("User32.dll", SetLastError = true)]
        public static extern uint SendInput(uint numInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] input, int structSize);

        [DllImport("User32.dll")]
        public static extern uint MapVirtualKey(uint uCode, uint uMapType);

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

        public enum VirtualKeyCodes : ushort {
            VK_BACKSPACE = 0x08,
            VK_TAB = 0x09,
            VK_ENTER = 0x0D,
            VK_SHIFT = 0x10,
            VK_CONTROL = 0x11,
            VK_ALT = 0x12,
            VK_PAUSE = 0x13,
            VK_CAPSLOCK = 0x14,
            VK_ESCAPE = 0x1B,
            VK_SPACE = 0x20,
            VK_PAGEUP = 0x21,
            VK_PAGEDOWN = 0x22,
            VK_END = 0x23,
            VK_HOME = 0x24,
            VK_LEFT = 0x25,
            VK_UP = 0x26,
            VK_RIGHT = 0x27,
            VK_DOWN = 0x28,
            VK_SELECT = 0x29,
            VK_PRINT = 0x2A,
            VK_PRINTSCR = 0x2C,
            VK_INSERT = 0x2D,
            VK_DELETE = 0x2E,
            VK_0 = 0x30,
            VK_1 = 0x31,
            VK_2 = 0x32,
            VK_3 = 0x33,
            VK_4 = 0x34,
            VK_5 = 0x35,
            VK_6 = 0x36,
            VK_7 = 0x37,
            VK_8 = 0x38,
            VK_9 = 0x39,
            VK_F1 = 0x70,
            VK_F2 = 0x71,
            VK_F3 = 0x72,
            VK_F4 = 0x73,
            VK_F5 = 0x74,
            VK_F6 = 0x75,
            VK_F7 = 0x76,
            VK_F8 = 0x77,
            VK_F9 = 0x78,
            VK_F10 = 0x79,
            VK_F11 = 0x7a,
            VK_F12 = 0x7b,
        }
    }
}