Skip to content

Instantly share code, notes, and snippets.

@taylor224
Last active November 13, 2017 15:24
Show Gist options
  • Save taylor224/11273a33948ae72438db to your computer and use it in GitHub Desktop.
Save taylor224/11273a33948ae72438db to your computer and use it in GitHub Desktop.
C# Touch Injection
private void TouchProcess()
{
movedx = 0; // Set Value to move of X
modedy = 0; // Set Value to move of Y
if (TouchIsInitialized == false)
{
contacts[0].PointerInfo.PointerFlags = PointerFlags.INRANGE | PointerFlags.UPDATE;
contacts[0] = MakePointerTouchInfo(relativex, relativey, 2, 1);
bool s = InjectTouch(contacts);
TouchIsInitialized = true;
}
if (order == "down")
{
rightdownmovecoord.Add(new Tuple<float, float>(handPosition.X - lastpointrightx, handPosition.Y - lastpointrighty));
if (contacts[0].PointerInfo.PointerFlags == (PointerFlags.INRANGE | PointerFlags.UPDATE))
{
contacts[0].PointerInfo.PointerFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
InjectTouch(contacts);
contacts[0].PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT;
}
else
{
contacts[0].PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT;
}
}
else // If Touch UP Order
{
if (rightdownmovecoord.Count > 0)
{
float totalx = 0;
float totaly = 0;
foreach (Tuple<float, float> item in rightdownmovecoord)
{
float x = item.Item1;
float y = item.Item2;
totalx += x;
totaly += y;
}
totalx = totalx / rightdownmovecoord.Count;
totaly = totaly / rightdownmovecoord.Count;
// If User want to make touch down event(Touch Click event)
if (totalx < 10 && totaly < 10)
{
contacts[0].PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT;
InjectTouch(contacts);
contacts[0].PointerInfo.PointerFlags = PointerFlags.INRANGE | PointerFlags.UP;
InjectTouch(contacts);
}
// Reset Right down log data
rightdownmovecoord = new List<Tuple<float, float>>();
}
// Make Touch Up
contacts[0].PointerInfo.PointerFlags = PointerFlags.INRANGE | PointerFlags.UPDATE;
}
contacts[0].Move(movedx, movedy);
bool s = InjectTouch(contacts);
}
private bool InjectTouch(PointerTouchInfo[] contactinfoe)
{
PointerTouchInfo[] pti = new PointerTouchInfo[1];
pti[0] = contactinfo[0];
bool s = TouchInjector.InjectTouchInput(1, pti);
return s;
}
private PointerTouchInfo MakePointerTouchInfo(int x, int y, int radius, uint id, uint orientation = 90, uint pressure = 32000)
{
PointerTouchInfo contact = new PointerTouchInfo();
contact.PointerInfo.pointerType = PointerInputType.TOUCH;
contact.TouchFlags = TouchFlags.NONE;
contact.Orientation = orientation;
contact.Pressure = pressure;
contact.PointerInfo.PointerFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
contact.TouchMasks = TouchMask.CONTACTAREA | TouchMask.ORIENTATION | TouchMask.PRESSURE;
contact.PointerInfo.PtPixelLocation.X = x;
contact.PointerInfo.PtPixelLocation.Y = y;
contact.PointerInfo.PointerId = id;
contact.ContactArea.left = x - radius;
contact.ContactArea.right = x + radius;
contact.ContactArea.top = y - radius;
contact.ContactArea.bottom = y + radius;
return contact;
}
@rakshith-ravi
Copy link

Hey. Does this require UWP? Would this work on a WPF or a winforms app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment