internal static extern bool DeviceIoControl( SafeFileHandle hDevice, EIOControlCode IoControlCode, ref INJECT_MOUSE_BUTTON_INPUT_REQUEST InBuffer, int nInBufferSize, ref INJECT_MOUSE_BUTTON_INPUT_REQUEST OutBuffer, int nOutBufferSize, out uint pBytesReturned, IntPtr Overlapped );
public bool InjectMouseButtonInput(ulong processId, ushort buttonFlags, ushort buttonData) { if (driverHandle == null) { throw new Exception( "Driver handle is null. Is the driver running and have you called Init() function?"); } INJECT_MOUSE_BUTTON_INPUT_REQUEST Request = new INJECT_MOUSE_BUTTON_INPUT_REQUEST(); Request.ProcessId = (IntPtr)processId; Request.ButtonFlags = buttonFlags; Request.ButtonData = buttonData; uint dummy = 0; bool status = MouInputIo.DeviceIoControl( driverHandle, EIOControlCode.IOCTL_INJECT_MOUSE_BUTTON_INPUT, ref Request, Marshal.SizeOf(Request), ref Request, 0, out dummy, IntPtr.Zero ); if (status) { Console.WriteLine("SUCCESS"); } else { Console.WriteLine("Failed"); Console.WriteLine(Connector.GetLastError()); } return(status); }
public bool InjectMouseButtonClick(ulong processId, ushort button, ulong durationInMilliseconds) { if (driverHandle == null) { throw new Exception( "Driver handle is null. Is the driver running and have you called Init() function?"); } ushort releaseButton = (ushort)ButtonCode.MOUSE_LEFT_BUTTON_DOWN; // Determine the release button flag based on the button. switch ((ButtonCode)button) { case ButtonCode.MOUSE_LEFT_BUTTON_DOWN: releaseButton = (ushort)ButtonCode.MOUSE_LEFT_BUTTON_UP; break; case ButtonCode.MOUSE_RIGHT_BUTTON_DOWN: releaseButton = (ushort)ButtonCode.MOUSE_RIGHT_BUTTON_UP; break; case ButtonCode.MOUSE_MIDDLE_BUTTON_DOWN: releaseButton = (ushort)ButtonCode.MOUSE_MIDDLE_BUTTON_UP; break; case ButtonCode.MOUSE_BUTTON_4_DOWN: releaseButton = (ushort)ButtonCode.MOUSE_BUTTON_4_UP; break; case ButtonCode.MOUSE_BUTTON_5_DOWN: releaseButton = (ushort)ButtonCode.MOUSE_BUTTON_5_UP; break; default: Console.WriteLine("Invalid button flag ", button); return(false); } INJECT_MOUSE_BUTTON_INPUT_REQUEST Request = new INJECT_MOUSE_BUTTON_INPUT_REQUEST(); Request.ProcessId = (IntPtr)processId; Request.ButtonFlags = button; Request.ButtonData = 0; uint dummy = 0; bool status = MouInputIo.DeviceIoControl( driverHandle, EIOControlCode.IOCTL_INJECT_MOUSE_BUTTON_INPUT, ref Request, Marshal.SizeOf(Request), ref Request, 0, out dummy, IntPtr.Zero ); if (!status) { Console.WriteLine("Failed"); Console.WriteLine(Connector.GetLastError()); return(false); } // Make a delay for durationInMilliseconds Thread.Sleep((int)durationInMilliseconds); Request.ButtonFlags = releaseButton; status = MouInputIo.DeviceIoControl( driverHandle, EIOControlCode.IOCTL_INJECT_MOUSE_BUTTON_INPUT, ref Request, Marshal.SizeOf(Request), ref Request, 0, out dummy, IntPtr.Zero ); if (!status) { Console.WriteLine("Failed"); Console.WriteLine(Connector.GetLastError()); return(false); } Console.WriteLine("Success"); return(status); }