using Microsoft.VisualBasic.Devices; using System; using System.Drawing; using System.Runtime.InteropServices; using System.Threading; //#include namespace ctrlv { class Program { // Win32 constants for mouse events private const int INPUT_MOUSE = 0; private const uint MOUSEEVENTF_LEFTDOWN = 0x0002; private const uint MOUSEEVENTF_LEFTUP = 0x0004; [StructLayout(LayoutKind.Sequential)] struct INPUT { public int type; public MOUSEINPUT mi; } // Define the POINT struct for WinAPI [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; } [DllImport("user32.dll")] private static extern bool GetCursorPos(out POINT lpPoint); // Import Win32 API functions [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [StructLayout(LayoutKind.Sequential)] struct MOUSEINPUT { public int dx; public int dy; public uint mouseData; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; } [DllImport("user32.dll")] private static extern IntPtr WindowFromPoint(POINT point); [DllImport("user32.dll", SetLastError = true)] private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); [STAThread ] static void Main() { IntPtr hWnd=0; try { // Get current mouse position if (!GetCursorPos(out POINT cursorPos)) { Console.WriteLine("Failed to get cursor position."); return; } // Get the window handle at the cursor position hWnd = WindowFromPoint(cursorPos); if (hWnd == IntPtr.Zero) { Console.WriteLine("No window found at the cursor position."); } else { Console.WriteLine($"Window handle at cursor: {hWnd}"); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } //Console.WriteLine("Click will happen in 2 seconds..."); //Thread.Sleep(2000); // Delay so you can move the mouse Point currentPos = Cursor.Position; //IntPtr hWnd = WindowFromPoint(currentPos); SetForegroundWindow(hWnd); Thread.Sleep(100); //Clipboard.SetText("testx"); MouseClick(); //MouseClick(); //MouseClick(); ////mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)currentPos.X, (uint)currentPos.Y, 0, 0); // Clipboard.GetText(); //SendKeys.SendWait("V"); SendKeys.SendWait("^V"); //SendKeys.SendWait("V"); //Console.WriteLine("Mouse click sent."); } static void MouseClick() { INPUT[] inputs = new INPUT[2]; // Mouse down inputs[0].type = INPUT_MOUSE; inputs[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN; Thread.Sleep(100); // Mouse up inputs[1].type = INPUT_MOUSE; inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTUP; // Send both events if (SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT))) == 0) { Console.WriteLine("Mouse click failed. Error: " + Marshal.GetLastWin32Error()); } } } }