Exemple #1
0
        /// <summary>
        /// Calls the Win32 SendInput method with a KeyDown and KeyUp message in the same input sequence in order to simulate a Key PRESS.
        ///
        /// </summary>
        /// <param name="keyCode">The KeyCode to press</param>
        public static void SimulateKeyPress(this KeyCode keyCode)
        {
            var input1 = new INPUT();

            input1.Type                    = 1U;
            input1.Data.Keyboard           = new KEYBDINPUT();
            input1.Data.Keyboard.Vk        = (ushort)keyCode;
            input1.Data.Keyboard.Scan      = 0;
            input1.Data.Keyboard.Flags     = 0U;
            input1.Data.Keyboard.Time      = 0U;
            input1.Data.Keyboard.ExtraInfo = IntPtr.Zero;
            var input2 = new INPUT();

            input2.Type                    = 1U;
            input2.Data.Keyboard           = new KEYBDINPUT();
            input2.Data.Keyboard.Vk        = (ushort)keyCode;
            input2.Data.Keyboard.Scan      = 0;
            input2.Data.Keyboard.Flags     = 2U;
            input2.Data.Keyboard.Time      = 0U;
            input2.Data.Keyboard.ExtraInfo = IntPtr.Zero;
            if ((int)NativeWin32.SendInput(2U, new INPUT[2] {
                input1,
                input2
            }, Marshal.SizeOf(typeof(INPUT))) == 0)
            {
                throw new Exception(string.Format("The key press simulation for {0} was not successful.", keyCode));
            }
        }
Exemple #2
0
        public void PressKey(Keys keyCode)
        {
            var structInput = new INPUT();

            structInput.type           = 1;
            structInput.ki.wScan       = 0;
            structInput.ki.time        = 0;
            structInput.ki.dwFlags     = 0;
            structInput.ki.dwExtraInfo = 0;
            // Key down the actual key-code

            structInput.ki.wVk = (ushort)keyCode; //VK.SHIFT etc.
            NativeWin32.SendInput(1, ref structInput, Marshal.SizeOf(structInput));
        }
Exemple #3
0
        public void ReleaseKey(Keys keyCode)
        {
            var structInput = new INPUT();

            structInput.type           = 1;
            structInput.ki.wScan       = 0;
            structInput.ki.time        = 0;
            structInput.ki.dwFlags     = 0;
            structInput.ki.dwExtraInfo = 0;

            // Key up the actual key-code
            structInput.ki.dwFlags = NativeWin32.KEYEVENTF_KEYUP;
            structInput.ki.wVk     = (ushort)keyCode;// (ushort)NativeWin32.VK.SNAPSHOT;//vk;
            NativeWin32.SendInput(1, ref structInput, Marshal.SizeOf(structInput));
        }
Exemple #4
0
        /// <summary>
        /// Calls the Win32 SendInput method with a stream of KeyDown and KeyUp messages in order to simulate uninterrupted text entry via the keyboard.
        ///
        /// </summary>
        /// <param name="text">The text to be simulated.</param>
        public static void SimulateTextEntry(string text)
        {
            if (text.Length > (long)int.MaxValue)
            {
                throw new ArgumentException(
                          string.Format("The text parameter is too long. It must be less than {0} characters.",
                                        (uint)int.MaxValue), "text");
            }
            byte[] bytes  = Encoding.ASCII.GetBytes(text);
            int    length = bytes.Length;
            var    inputs = new INPUT[length * 2];

            for (int index = 0; index < length; ++index)
            {
                ushort num    = bytes[index];
                var    input1 = new INPUT();
                input1.Type                    = 1U;
                input1.Data.Keyboard           = new KEYBDINPUT();
                input1.Data.Keyboard.Vk        = 0;
                input1.Data.Keyboard.Scan      = num;
                input1.Data.Keyboard.Flags     = 4U;
                input1.Data.Keyboard.Time      = 0U;
                input1.Data.Keyboard.ExtraInfo = IntPtr.Zero;
                var input2 = new INPUT();
                input2.Type                    = 1U;
                input2.Data.Keyboard           = new KEYBDINPUT();
                input2.Data.Keyboard.Vk        = 0;
                input2.Data.Keyboard.Scan      = num;
                input2.Data.Keyboard.Flags     = 6U;
                input2.Data.Keyboard.Time      = 0U;
                input2.Data.Keyboard.ExtraInfo = IntPtr.Zero;
                if ((num & 65280) == 57344)
                {
                    input1.Data.Keyboard.Flags |= 1U;
                    input2.Data.Keyboard.Flags |= 1U;
                }
                inputs[2 * index]     = input1;
                inputs[2 * index + 1] = input2;
            }
            var num1 = (int)NativeWin32.SendInput((uint)(length * 2), inputs, Marshal.SizeOf(typeof(INPUT)));
        }