Skip to content

Instantly share code, notes, and snippets.

@rvrsh3ll
Created November 23, 2024 13:21
Show Gist options
  • Save rvrsh3ll/5f29cfa541717921a08c9ca65539cbfe to your computer and use it in GitHub Desktop.
Save rvrsh3ll/5f29cfa541717921a08c9ca65539cbfe to your computer and use it in GitHub Desktop.
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public uint type;
public MOUSEINPUT mi;
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
const int INPUT_MOUSE = 0;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
public static void ClickAt(int x, int y)
{
INPUT[] input = new INPUT[2];
input[0].type = INPUT_MOUSE;
input[0].mi.dx = x;
input[0].mi.dy = y;
input[0].mi.mouseData = 0;
input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
input[0].mi.time = 0;
input[0].mi.dwExtraInfo = IntPtr.Zero;
input[1].type = INPUT_MOUSE;
input[1].mi.dx = x;
input[1].mi.dy = y;
input[1].mi.mouseData = 0;
input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
input[1].mi.time = 0;
input[1].mi.dwExtraInfo = IntPtr.Zero;
SendInput((uint)input.Length, input, Marshal.SizeOf(typeof(INPUT)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment