using System; using System.ComponentModel; using System.Runtime.InteropServices; // Native Unicode input avoids COM SendKeys timing and clipboard-paste restrictions. public static class ChromeKeyboard { [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr h, out uint processId); [DllImport("kernel32.dll")] private static extern uint GetCurrentThreadId(); [DllImport("user32.dll")] private static extern bool AttachThreadInput(uint source, uint target, bool attach); [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr h); [DllImport("user32.dll")] private static extern bool BringWindowToTop(IntPtr h); [DllImport("user32.dll")] private static extern void keybd_event(byte key, byte scan, uint flags, UIntPtr extra); public static void ActivateWindow(IntPtr window) { if (GetForegroundWindow() == window) return; // A synthetic Alt press is the documented user-input signal Windows // uses to allow a background process to transfer foreground focus. keybd_event(0x12, 0, 0, UIntPtr.Zero); keybd_event(0x12, 0, 2, UIntPtr.Zero); // SetForegroundWindow can report success before Windows has actually // changed the foreground window. Only return after verifying the HWND. SetForegroundWindow(window); if (GetForegroundWindow() == window) return; uint ignored; uint current = GetCurrentThreadId(); uint foreground = GetWindowThreadProcessId(GetForegroundWindow(), out ignored); uint target = GetWindowThreadProcessId(window, out ignored); bool attachedForeground = foreground != current && AttachThreadInput(current, foreground, true); bool attachedTarget = target != current && target != foreground && AttachThreadInput(current, target, true); try { BringWindowToTop(window); SetForegroundWindow(window); } finally { if (attachedTarget) AttachThreadInput(current, target, false); if (attachedForeground) AttachThreadInput(current, foreground, false); } } [StructLayout(LayoutKind.Sequential)] private struct KeyboardInput { public ushort key, scan; public uint flags, time; public UIntPtr extra; } [StructLayout(LayoutKind.Sequential)] private struct MouseInput { public int x, y; public uint data, flags, time; public UIntPtr extra; } [StructLayout(LayoutKind.Explicit)] private struct InputUnion { [FieldOffset(0)] public KeyboardInput keyboard; [FieldOffset(0)] public MouseInput mouse; } [StructLayout(LayoutKind.Sequential)] private struct Input { public uint type; public InputUnion data; } [DllImport("user32.dll", SetLastError = true)] private static extern uint SendInput(uint count, Input[] inputs, int size); public static void TypeText(string text) { var inputs = new Input[text.Length * 2]; for (int i = 0; i < text.Length; i++) { inputs[i * 2].type = inputs[i * 2 + 1].type = 1; inputs[i * 2].data.keyboard.scan = inputs[i * 2 + 1].data.keyboard.scan = text[i]; inputs[i * 2].data.keyboard.flags = 4; inputs[i * 2 + 1].data.keyboard.flags = 6; } if (SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(Input))) != inputs.Length) throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not type the complete image prompt into Chrome."); } }