/// <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); } Win32.INPUT[] inputs = new Win32.INPUT[text.Length * 2]; Win32.KEYBDINPUT kbi = new Win32.KEYBDINPUT(); for (int x = 0; x < text.Length; x++) { kbi.wScan = text[x]; kbi.dwFlags = Win32.KEYEVENTF_UNICODE; inputs[x * 2].mkhi.ki = kbi; inputs[x * 2].type = Win32.INPUT_KEYBOARD; kbi.dwFlags = Win32.KEYEVENTF_KEYUP | Win32.KEYEVENTF_UNICODE; inputs[x * 2 + 1].mkhi.ki = kbi; inputs[x * 2 + 1].type = Win32.INPUT_KEYBOARD; } uint success = Win32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0])); ci.DetachFromWindow(); return(success == inputs.Length); } return(false); }
/// <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)) { Win32.INPUT[] inputs = new Win32.INPUT[keys.Length * 2]; Win32.KEYBDINPUT kbi = new Win32.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 = Win32.INPUT_KEYBOARD; kbi.dwFlags = Win32.KEYEVENTF_KEYUP; inputs[x * 2 + 1].mkhi.ki = kbi; inputs[x * 2 + 1].type = Win32.INPUT_KEYBOARD; } uint success = Win32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0])); ci.DetachFromWindow(); return(success == inputs.Length); } return(false); }