Emulate Screen Taps

You can simulate user activity on your touch-screen device by using the mouse_event API to send the system screen taps. There are a number of reasons you might want to do this from testing your application through to automating some third-party application, the following is example code to send a tap (and release) to any screen co-ordinate:-


[DllImport(“coredll”)]
private static extern void mouse_event(MOUSEEVENTF dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

[Flags()]
private enum MOUSEEVENTF
{
MOVE = 0x1, /* mouse move */
LEFTDOWN = 0x2, /* left button down */
LEFTUP = 0x4, /*left button up */
RIGHTDOWN = 0x8, /*right button down */
RIGHTUP = 0x10, /*right button up */
MIDDLEDOWN = 0x20, /*middle button down */
MIDDLEUP = 0x40, /* middle button up */
WHEEL = 0x800, /*wheel button rolled */
VIRTUALDESK = 0x4000, /* map to entrire virtual desktop */
ABSOLUTE = 0x8000, /* absolute move */
TOUCH = 0x100000, /* absolute move */
}

public void SendTap(int x, int y)
{
mouse_event(MOUSEEVENTF.LEFTDOWN | MOUSEEVENTF.ABSOLUTE, (int)((65535 / Screen.PrimaryScreen.Bounds.Width) * x), (int)((65535 / Screen.PrimaryScreen.Bounds.Height) * y), 0, 0);
mouse_event(MOUSEEVENTF.LEFTUP, 0, 0, 0, 0);
}


 


The mouse_event method is fairly straight-forward, one thing to notice is that it doesn’t accept screen co-ordinates but rather a value from 0 to FFFF in each direction, where FFFF, FFFF is the bottom right corner.