Beispiel #1
0
 /// <summary>
 /// Send text to client as keypresses.  Restores and activates window.
 /// </summary>
 /// <param name="client">Target client.</param>
 /// <param name="text">String to send.</param>
 /// <returns>True if all text was successfully sent.</returns>
 public static bool SendText(int client, string text)
 {
     if (string.IsNullOrEmpty(text)) return false;
     ClientInfo ci;
     if (ClientInfoCollection.GetClient(client, out ci))
     {
         if (!ci.PrepareWindowForInput())
         {
             ci.DetachFromWindow();
             return false;
         }
         NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[text.Length * 2];
         NativeMethods.KEYBDINPUT kbi = new NativeMethods.KEYBDINPUT();
         for (int x = 0; x < text.Length; x++)
         {
             kbi.wScan = text[x];
             kbi.dwFlags = NativeMethods.KEYEVENTF_UNICODE;
             inputs[x * 2].mkhi.ki = kbi;
             inputs[x * 2].type = NativeMethods.INPUT_KEYBOARD;
             kbi.dwFlags = NativeMethods.KEYEVENTF_KEYUP | NativeMethods.KEYEVENTF_UNICODE;
             inputs[x * 2 + 1].mkhi.ki = kbi;
             inputs[x * 2 + 1].type = NativeMethods.INPUT_KEYBOARD;
         }
         uint success = NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0]));
         ci.DetachFromWindow();
         return success == inputs.Length;
     }
     return false;
 }
Beispiel #2
0
        /// <summary>
        /// Send text to client as keypresses.  Restores and activates window.
        /// </summary>
        /// <param name="client">Target client.</param>
        /// <param name="text">String to send.</param>
        /// <returns>True if all text was successfully sent.</returns>
        public static bool SendText(int client, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(false);
            }
            ClientInfo ci;

            if (ClientInfoCollection.GetClient(client, out ci))
            {
                if (!ci.PrepareWindowForInput())
                {
                    ci.DetachFromWindow();
                    return(false);
                }
                NativeMethods.INPUT[]    inputs = new NativeMethods.INPUT[text.Length * 2];
                NativeMethods.KEYBDINPUT kbi    = new NativeMethods.KEYBDINPUT();
                for (int x = 0; x < text.Length; x++)
                {
                    kbi.wScan                 = text[x];
                    kbi.dwFlags               = NativeMethods.KEYEVENTF_UNICODE;
                    inputs[x * 2].mkhi.ki     = kbi;
                    inputs[x * 2].type        = NativeMethods.INPUT_KEYBOARD;
                    kbi.dwFlags               = NativeMethods.KEYEVENTF_KEYUP | NativeMethods.KEYEVENTF_UNICODE;
                    inputs[x * 2 + 1].mkhi.ki = kbi;
                    inputs[x * 2 + 1].type    = NativeMethods.INPUT_KEYBOARD;
                }
                uint success = NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0]));
                ci.DetachFromWindow();
                return(success == inputs.Length);
            }
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// Send array of System.Window.Forms.Keys to client as keypresses.  Restores and activates window.
        /// </summary>
        /// <param name="client">Target client.</param>
        /// <param name="keys">System.Window.Forms.Keys to send.</param>
        /// <returns>True if all keys were successfully sent.</returns>
        public static bool SendKeys(int client, Keys[] keys)
        {
            if (keys.Length == 0)
            {
                return(false);
            }
            ClientInfo ci;

            if (ClientInfoCollection.GetClient(client, out ci))
            {
                NativeMethods.INPUT[]    inputs = new NativeMethods.INPUT[keys.Length * 2];
                NativeMethods.KEYBDINPUT kbi    = new NativeMethods.KEYBDINPUT();

                if (!ci.PrepareWindowForInput())
                {
                    ci.DetachFromWindow();
                    return(false);
                }

                for (int x = 0; x < keys.Length; x++)
                {
                    kbi.dwFlags               = 0;
                    kbi.wVk                   = (ushort)keys[x];
                    inputs[x * 2].mkhi.ki     = kbi;
                    inputs[x * 2].type        = NativeMethods.INPUT_KEYBOARD;
                    kbi.dwFlags               = NativeMethods.KEYEVENTF_KEYUP;
                    inputs[x * 2 + 1].mkhi.ki = kbi;
                    inputs[x * 2 + 1].type    = NativeMethods.INPUT_KEYBOARD;
                }
                uint success = NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0]));
                ci.DetachFromWindow();
                return(success == inputs.Length);
            }
            return(false);
        }
        public void WriteText(string characters)
        {
            NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[2 * characters.Length];
            for (int i = 0; i < inputs.Length; i++)
            {
                var ki = new NativeMethods.KEYBDINPUT();
                ki.dwFlags = NativeMethods.KEYEVENTF.UNICODE;
                if (i % 2 == 1)
                    ki.dwFlags |= NativeMethods.KEYEVENTF.KEYUP;
                ki.wScan = (short)characters[i / 2];

                var input = new NativeMethods.INPUT();
                input.type = NativeMethods.INPUT_KEYBOARD;
                input.U.ki = ki;

                inputs[i] = input;
            }

            if (NativeMethods.SendInput((uint)inputs.Length, inputs, NativeMethods.INPUT.Size) == 0)
                throw new Win32Exception();
        }
        private void PressOrReleaseKey(VirtualKeyShort keyCode, bool down)
        {
            var ki = new NativeMethods.KEYBDINPUT();
            ki.wVk = keyCode;
            if (!down)
                ki.dwFlags = NativeMethods.KEYEVENTF.KEYUP;

            var input = new NativeMethods.INPUT();
            input.type = NativeMethods.INPUT_KEYBOARD;
            input.U.ki = ki;

            NativeMethods.INPUT[] inputs = { input };

            if (NativeMethods.SendInput((uint)inputs.Length, inputs, NativeMethods.INPUT.Size) == 0)
                throw new Win32Exception();
        }
Beispiel #6
0
        private static void LogKeyboardInput(NativeMethods.KEYBDINPUT input)
        {
            var isExtendedKey = (input.dwFlags & NativeMethods.KEYEVENTF_EXTENDEDKEY) != 0;
            var isKeyUp       = (input.dwFlags & NativeMethods.KEYEVENTF_KEYUP) != 0;
            var isUnicode     = (input.dwFlags & NativeMethods.KEYEVENTF_UNICODE) != 0;
            var isScanCode    = (input.dwFlags & NativeMethods.KEYEVENTF_SCANCODE) != 0;

            if (isUnicode && input.wVk != 0)
            {
                Debug.WriteLine("UNEXPECTED: if KEYEVENTF_UNICODE flag is specified then wVk must be 0.");
                return;
            }

            var builder = SharedPools.Default <StringBuilder>().AllocateAndClear();

            builder.Append("Send Key: ");

            char ch;

            if (isUnicode || isScanCode)
            {
                builder.Append(input.wScan.ToString("x4"));
                ch = (char)input.wScan;
            }
            else
            {
                builder.Append(input.wVk.ToString("x4"));
                ch = (char)(NativeMethods.MapVirtualKey(input.wVk, NativeMethods.MAPVK_VK_TO_CHAR) & 0x0000ffff);
            }

            // Append code and printable character
            builder.Append(' ');
            AppendPrintableChar(ch, builder);

            if (!isUnicode && !isScanCode && input.wVk <= byte.MaxValue)
            {
                AppendVirtualKey((byte)input.wVk, builder);
            }

            // Append flags
            if (input.dwFlags == 0)
            {
                builder.Append("[none]");
            }
            else
            {
                builder.Append('[');

                if (isExtendedKey)
                {
                    AppendFlag("extended", builder);
                }

                if (isKeyUp)
                {
                    AppendFlag("key up", builder);
                }

                if (isUnicode)
                {
                    AppendFlag("unicode", builder);
                }

                if (isScanCode)
                {
                    AppendFlag("scan code", builder);
                }

                builder.Append(']');
            }

            Debug.WriteLine(builder.ToString());

            SharedPools.Default <StringBuilder>().ClearAndFree(builder);
        }