aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SimulateTouch.UI/Native/TouchSimulate.cs
blob: cbe59b35abc3d3af86bf664a7038629cc7675691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace Tango.SimulateTouch.UI.Native
{
    public class TouchSimulate
    {
        public TouchSimulate()
        {
            int x = this.GetRandomSeed().Next(50, 1680 - 100);
            int y = this.GetRandomSeed().Next(50, 1080 - 100);
            SimulateTouch(x, y);
        } 
        private Random GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return new Random(BitConverter.ToInt32(bytes, 0));
        }

        public void SimulateTouch(int x, int y)
        {
            // Touch Down Simulate
            PointerTouchInfo contact = MakePointerTouchInfo(x, y, 5, 1);
            PointerFlags oFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
            contact.PointerInfo.PointerFlags = oFlags;
            bool bIsSuccess = TouchInjector.InjectTouchInput(1, new[] { contact });

            // Touch Move Simulate
            int nMoveIntervalX = this.GetRandomSeed().Next(-60, 60);
            int nMoveIntervalY = this.GetRandomSeed().Next(-60, 60);
            contact.Move(nMoveIntervalX, nMoveIntervalY);
            oFlags = PointerFlags.INRANGE | PointerFlags.INCONTACT | PointerFlags.UPDATE;
            contact.PointerInfo.PointerFlags = oFlags;
            TouchInjector.InjectTouchInput(1, new[] { contact });

            // Touch Up Simulate
            contact.PointerInfo.PointerFlags = PointerFlags.UP;
            TouchInjector.InjectTouchInput(1, new[] { contact });
        }
        private PointerTouchInfo MakePointerTouchInfo(int x, int y, int radius,
            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.TouchMasks = TouchMask.CONTACTAREA | TouchMask.ORIENTATION | TouchMask.PRESSURE;
            contact.PointerInfo.PtPixelLocation.X = x;
            contact.PointerInfo.PtPixelLocation.Y = y;
            uint unPointerId = IdGenerator.GetUinqueUInt();
            Console.WriteLine("PointerId    " + unPointerId);
            contact.PointerInfo.PointerId = unPointerId;
            contact.ContactArea.left = x - radius;
            contact.ContactArea.right = x + radius;
            contact.ContactArea.top = y - radius;
            contact.ContactArea.bottom = y + radius;
            return contact;
        }
    }
}