Doing this in XNA is the same way as normal C#. To use the code below, make sure you are using the System.Runtime.InteropServices;
namespace.
Disclaimer: I would consider this somewhat "dirty" code, it uses user32.dll
to invoke the click in Windows, but it is really the only way. (Adapted from here)
First off, you need 4 constants to easily use different types of clicks:
private const int MouseEvent_LeftDown = 0x02;
private const int MouseEvent_LeftUp = 0x04;
private const int MouseEvent_RightDown = 0x08;
private const int MouseEvent_RightUp = 0x10;
You will then need to hook into the mouse events:
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void MouseEvent(uint dwFlags, uint dx, uint dy, uint cButtons,uint dwExtraInfo);
You can now write methods to create mouse clicks:
LeftClick(int x, int y)
{
MouseEvent(MouseEvent_LeftDown | MouseEvent_LeftUp, x, y, 0, 0);
}
RightClick(int x, int y)
{
MouseEvent(MouseEvent_RightDown | MouseEvent_RightUp, x, y, 0, 0);
}
...etc, etc. You can see how you can adapt this to also create hold/drag events to mimic more functionality.
Note: I'm not sure if this will register with the MouseState
, but that shouldn't be needed because you are trying to use it to control the computer, and the game should never need to use the mouse state.