Esempio n. 1
0
        /// <summary>
        /// Sends text as keyboard input to the operating system, character by character.
        /// </summary>
        /// <remarks>
        /// Use '\r' characters to send enter key strokes. The character '\n' is ignored.
        /// </remarks>
        /// <param name="text">The text to send.</param>
        public static void SendKeyboardInput(string text)
        {
            // create input structs for normal texts
            var textInputs = new Win32.INPUT[2];

            // create two keyboard inputs
            var firstInput = new Win32.INPUT();
            firstInput.type = Win32.Constants.INPUT_KEYBOARD;
            firstInput.ki = new Win32.KEYBDINPUT();
            firstInput.ki.dwFlags = Win32.Constants.KEYEVENTF_UNICODE;
            textInputs[0] = firstInput;

            var secondInput = new Win32.INPUT();
            secondInput.type = Win32.Constants.INPUT_KEYBOARD;
            secondInput.ki = new Win32.KEYBDINPUT();
            secondInput.ki.dwFlags = Win32.Constants.KEYEVENTF_UNICODE | Win32.Constants.KEYEVENTF_KEYUP;
            textInputs[1] = secondInput;

            // create input structs for special/control chars
            var controlCharsInput = new Win32.INPUT[2];

            // create two keyboard inputs
            var firstControlCharInput = new Win32.INPUT();
            firstControlCharInput.type = Win32.Constants.INPUT_KEYBOARD;
            firstControlCharInput.ki = new Win32.KEYBDINPUT();
            firstControlCharInput.ki.dwFlags = Win32.Constants.KEYEVENTF_SCANCODE;
            controlCharsInput[0] = firstControlCharInput;

            var secondControlCharInput = new Win32.INPUT();
            secondControlCharInput.type = Win32.Constants.INPUT_KEYBOARD;
            secondControlCharInput.ki = new Win32.KEYBDINPUT();
            secondControlCharInput.ki.dwFlags = Win32.Constants.KEYEVENTF_SCANCODE | Win32.Constants.KEYEVENTF_KEYUP;
            controlCharsInput[1] = secondControlCharInput;

            // for each character, send input
            for (int i = 0; i < text.Length; i++)
            {
                // treat special characters differently:
                // send a keyboard scan code for the enter key for each \r character
                if (text[i] == '\r')
                {
                    controlCharsInput[0].ki.wScan = 0x1c;
                    controlCharsInput[1].ki.wScan = 0x1c;
                    Win32.SendInput(2, controlCharsInput, Marshal.SizeOf(firstControlCharInput));
                    continue;
                }
                else if (text[i] == '\n')
                {
                    // ignore this, because we've already processed \r
                    // note: this logic doesn't work for *nix-like input that only sends \n
                    continue;
                }

                // send the unicode character
                textInputs[0].ki.wScan = text[i];
                textInputs[1].ki.wScan = text[i];
                Win32.SendInput(2, textInputs, Marshal.SizeOf(firstInput));
            }
        }
Esempio n. 2
0
        private static void SendMouseInput(Win32.MOUSEINPUT mouseInput)
        {
            // create an input structure
            Win32.INPUT input = new Win32.INPUT();
            input.type = Win32.Constants.INPUT_MOUSE;
            input.mi = mouseInput;

            // send
            uint result = Win32.SendInput(1, new[] { input }, Marshal.SizeOf(input));

            // check for errors
            var lastError = Win32.GetLastError();
            if (lastError != 0)
            {
                throw new Exception("Error while sending mouse input: " + lastError);
            }
        }