Exemplo n.º 1
0
		static extern UInt32 SendInput(UInt32 numberOfInputs, INPUT[] inputs, Int32 sizeOfInputStructure);
Exemplo n.º 2
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 > UInt32.MaxValue / 2) throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text");

			var chars = text.ToCharArray(); // UTF8Encoding.ASCII.GetBytes(text);
			var len = chars.Length;
			INPUT[] inputList = new INPUT[len * 2];
			for (int x = 0; x < len; x++)
			{
				UInt16 scanCode = chars[x];
				var down = new INPUT();
				var up = new INPUT();

				if (scanCode < 32)
				{
					// map the scan code to a Vk so we can deal with special control chars, e.g. Tab and Enter
					ushort vk = (ushort)VkKeyScan((char)chars[x]);
					ushort state = (ushort)((vk & (ushort)0xff00) >> 8);
					vk = (ushort)(vk & (ushort)0xff);

					down.Type = (UInt32)InputType.KEYBOARD;
					down.Data.Keyboard = new KEYBDINPUT();
					down.Data.Keyboard.Vk = vk;
					down.Data.Keyboard.Scan = 0;
					down.Data.Keyboard.Flags = (UInt32)0;
					down.Data.Keyboard.Time = 0;
					down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

					up.Type = (UInt32)InputType.KEYBOARD;
					up.Data.Keyboard = new KEYBDINPUT();
					up.Data.Keyboard.Vk = vk;
					up.Data.Keyboard.Scan = 0;
					up.Data.Keyboard.Flags = (UInt32)(KeyboardFlag.KEYUP);
					up.Data.Keyboard.Time = 0;
					up.Data.Keyboard.ExtraInfo = IntPtr.Zero;
				}
				else
				{
					down.Type = (UInt32)InputType.KEYBOARD;
					down.Data.Keyboard = new KEYBDINPUT();
					down.Data.Keyboard.Vk = 0;
					down.Data.Keyboard.Scan = scanCode;
					down.Data.Keyboard.Flags = (UInt32)KeyboardFlag.UNICODE;
					down.Data.Keyboard.Time = 0;
					down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

					up.Type = (UInt32)InputType.KEYBOARD;
					up.Data.Keyboard = new KEYBDINPUT();
					up.Data.Keyboard.Vk = 0;
					up.Data.Keyboard.Scan = scanCode;
					up.Data.Keyboard.Flags = (UInt32)(KeyboardFlag.KEYUP | KeyboardFlag.UNICODE);
					up.Data.Keyboard.Time = 0;
					up.Data.Keyboard.ExtraInfo = IntPtr.Zero;
				}

				// Handle extended keys:
				// If the scan code is preceded by a prefix byte that has the value 0xE0 (224),
				// we need to include the KEYEVENTF_EXTENDEDKEY flag in the Flags property. 
				if ((scanCode & 0xFF00) == 0xE000)
				{
					down.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY;
					up.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY;
				}

				inputList[2 * x] = down;
				inputList[2 * x + 1] = up;
			}

			var numberOfSuccessfulSimulatedInputs = SendInput((UInt32)len * 2, inputList, Marshal.SizeOf(typeof(INPUT)));
		}
Exemplo n.º 3
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 VirtualKeyCode to press</param>
		public static void SimulateKeyPress(VirtualKeyCode keyCode)
		{
			var down = new INPUT();
			down.Type = (UInt32)InputType.KEYBOARD;
			down.Data.Keyboard = new KEYBDINPUT();
			down.Data.Keyboard.Vk = (UInt16)keyCode;
			down.Data.Keyboard.Scan = 0;
			down.Data.Keyboard.Flags = 0;
			down.Data.Keyboard.Time = 0;
			down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

			var up = new INPUT();
			up.Type = (UInt32)InputType.KEYBOARD;
			up.Data.Keyboard = new KEYBDINPUT();
			up.Data.Keyboard.Vk = (UInt16)keyCode;
			up.Data.Keyboard.Scan = 0;
			up.Data.Keyboard.Flags = (UInt32)KeyboardFlag.KEYUP;
			up.Data.Keyboard.Time = 0;
			up.Data.Keyboard.ExtraInfo = IntPtr.Zero;

			INPUT[] inputList = new INPUT[2];
			inputList[0] = down;
			inputList[1] = up;

			var numberOfSuccessfulSimulatedInputs = SendInput(2, inputList, Marshal.SizeOf(typeof(INPUT)));
			if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key press simulation for {0} was not successful.", keyCode));
		}
Exemplo n.º 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 > UInt32.MaxValue / 2) throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text");

            var chars = UTF8Encoding.ASCII.GetBytes(text);
            var len = chars.Length;
            INPUT[] inputList = new INPUT[len * 2];
            for (int x = 0; x < len; x++)
            {
                UInt16 scanCode = chars[x];

                var down = new INPUT();
                down.Type = (UInt32)InputType.KEYBOARD;
                down.Data.Keyboard = new KEYBDINPUT();
                down.Data.Keyboard.VirtualKey = 0;
                down.Data.Keyboard.Scan = scanCode;
                down.Data.Keyboard.Flags = (UInt32)KeyboardFlag.UNICODE;
                down.Data.Keyboard.Time = 0;
                down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

                var up = new INPUT();
                up.Type = (UInt32)InputType.KEYBOARD;
                up.Data.Keyboard = new KEYBDINPUT();
                up.Data.Keyboard.VirtualKey = 0;
                up.Data.Keyboard.Scan = scanCode;
                up.Data.Keyboard.Flags = (UInt32)(KeyboardFlag.KEYUP | KeyboardFlag.UNICODE);
                up.Data.Keyboard.Time = 0;
                up.Data.Keyboard.ExtraInfo = IntPtr.Zero;

                // Handle extended keys:
                // If the scan code is preceded by a prefix byte that has the value 0xE0 (224),
                // we need to include the KEYEVENTF_EXTENDEDKEY flag in the Flags property. 
                if ((scanCode & 0xFF00) == 0xE000)
                {
                    down.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY;
                    up.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY;
                }

                inputList[2*x] = down;
                inputList[2*x + 1] = up;

            }

            var numberOfSuccessfulSimulatedInputs = SendInput((UInt32)len*2, inputList, Marshal.SizeOf(typeof(INPUT)));
        }
        /// <summary>
        /// Calls the Win32 SendInput method to simulate a Key DOWN.
        /// </summary>
        /// <param name="keyCode">The VirtualKeyCode to press</param>
        public static void SimulateKeyDown(VirtualKeyCode keyCode)
        {
            var down = new INPUT();
            down.Type = (UInt32)InputType.KEYBOARD;
            down.Data.Keyboard = new KEYBDINPUT();
            down.Data.Keyboard.Vk = (UInt16)keyCode;
            //down.Data.Keyboard.Scan = 0;
            down.Data.Keyboard.Scan = (ushort)MapVirtualKey((UInt16)keyCode, 0);
            down.Data.Keyboard.Flags = 0;
            down.Data.Keyboard.Time = 0;
            down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

            INPUT[] inputList = new INPUT[1];
            inputList[0] = down;

            var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT)));
            if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key down simulation for {0} was not successful.", keyCode));
        }
Exemplo n.º 6
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 > uint.MaxValue / 2)
            {
                throw new ArgumentException(
                          string.Format("The text parameter is too long. It must be less than {0} characters.",
                                        uint.MaxValue / 2), "text");
            }

            var chars     = text.ToCharArray(); // UTF8Encoding.ASCII.GetBytes(text);
            var len       = chars.Length;
            var inputList = new INPUT[len * 2];

            for (var x = 0; x < len; x++)
            {
                ushort scanCode = chars[x];
                var    down     = new INPUT();
                var    up       = new INPUT();

                if (scanCode < 32)
                {
                    // map the scan code to a Vk so we can deal with special control chars, e.g. Tab and Enter
                    var vk    = (ushort)VkKeyScan(chars[x]);
                    var state = (ushort)((vk & 0xff00) >> 8);
                    vk = (ushort)(vk & 0xff);

                    down.Type                    = (uint)InputType.KEYBOARD;
                    down.Data.Keyboard           = new KEYBDINPUT();
                    down.Data.Keyboard.Vk        = vk;
                    down.Data.Keyboard.Scan      = 0;
                    down.Data.Keyboard.Flags     = 0;
                    down.Data.Keyboard.Time      = 0;
                    down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

                    up.Type                    = (uint)InputType.KEYBOARD;
                    up.Data.Keyboard           = new KEYBDINPUT();
                    up.Data.Keyboard.Vk        = vk;
                    up.Data.Keyboard.Scan      = 0;
                    up.Data.Keyboard.Flags     = (uint)KeyboardFlag.KEYUP;
                    up.Data.Keyboard.Time      = 0;
                    up.Data.Keyboard.ExtraInfo = IntPtr.Zero;
                }
                else
                {
                    down.Type                    = (uint)InputType.KEYBOARD;
                    down.Data.Keyboard           = new KEYBDINPUT();
                    down.Data.Keyboard.Vk        = 0;
                    down.Data.Keyboard.Scan      = scanCode;
                    down.Data.Keyboard.Flags     = (uint)KeyboardFlag.UNICODE;
                    down.Data.Keyboard.Time      = 0;
                    down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

                    up.Type                    = (uint)InputType.KEYBOARD;
                    up.Data.Keyboard           = new KEYBDINPUT();
                    up.Data.Keyboard.Vk        = 0;
                    up.Data.Keyboard.Scan      = scanCode;
                    up.Data.Keyboard.Flags     = (uint)(KeyboardFlag.KEYUP | KeyboardFlag.UNICODE);
                    up.Data.Keyboard.Time      = 0;
                    up.Data.Keyboard.ExtraInfo = IntPtr.Zero;
                }

                // Handle extended keys:
                // If the scan code is preceded by a prefix byte that has the value 0xE0 (224),
                // we need to include the KEYEVENTF_EXTENDEDKEY flag in the Flags property.
                if ((scanCode & 0xFF00) == 0xE000)
                {
                    down.Data.Keyboard.Flags |= (uint)KeyboardFlag.EXTENDEDKEY;
                    up.Data.Keyboard.Flags   |= (uint)KeyboardFlag.EXTENDEDKEY;
                }

                inputList[2 * x]     = down;
                inputList[2 * x + 1] = up;
            }

            var numberOfSuccessfulSimulatedInputs = SendInput((uint)len * 2, inputList, Marshal.SizeOf(typeof(INPUT)));
        }