Simple touch/swipe input function in Windows

2017-03-28

C++ · Input · Windows

1 minute

For self reference:

Simulate a touch input down, drag, up programmatically.

#include <Windows.h>
BOOL TouchInput(POINTER_FLAGS pointerFlags, int x, int y)
{
POINTER_TOUCH_INFO contact = { 0 };
contact.pointerInfo.pointerType = PT_TOUCH;
contact.pointerInfo.pointerFlags = pointerFlags;
contact.pointerInfo.ptPixelLocation.x = x;
contact.pointerInfo.ptPixelLocation.y = y;
contact.pointerInfo.pointerId = 0;
return InjectTouchInput(1, &contact);
}
BOOL TouchInputDown(int x, int y)
{
return TouchInput(POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_DOWN, x, y);
}
BOOL TouchInputDrag(int x, int y)
{
return TouchInput(POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_UPDATE, x, y);
}
BOOL TouchInputUp(int x, int y)
{
return TouchInput(POINTER_FLAG_INRANGE | POINTER_FLAG_UP, x, y);
}
// Left to right swipe simulation (app switch in Win8/8.1)
void SimulateTouchSwipe()
{
InitializeTouchInjection(1, TOUCH_FEEDBACK_NONE);
int x = 0, y = 10;
TouchInputDown(0, 10);
for (int itr = 0; itr < 100; ++itr) {
x = (itr * 10);
TouchInputDrag(x, y);
}
TouchInputUp(x, y);
}