Search code examples
c#windowswinapiwindows-10tablet

Register a touch event on Windows 10 (without a mouse click or actual touch)


I'm developing an application for windows 10 (PC device) that uses the capability of "tablet mode".

I want to register a touch event on a given x and y position (without a mouse click or actual touch). What are the APIs for handling this kind of action in C#?


Solution

  • If you want to simulate touch event in Windows there is a Win32 API for it.

    MSDN: InjectTouchInput

    NuGet: C# wrapper

    You can simulate absolutely any combination of pointer input that Windows 10 is capable of, including pressure, contact area, pens, touch, mouse, multitouch, etc.

    Sample code:

    //usings:
    using TCD.System.TouchInjection;
    using static TCD.System.TouchInjection.TouchInjector;
    
    
    // code:
    
    InitializeTouchInjection();
    
    var pointer = new PointerTouchInfo();
    
    //We can add different additional touch data
    pointer.TouchMasks = TouchMask.PRESSURE;
    pointer.Pressure = 100;
    
    
    //Pointer ID is for gesture tracking
    pointer.PointerInfo.PointerId = 1;
    pointer.PointerInfo.pointerType = PointerInputType.TOUCH;
    
    pointer.PointerInfo.PtPixelLocation.X = 200;
    pointer.PointerInfo.PtPixelLocation.Y = 200;
    
    pointer.PointerInfo.PointerFlags = PointerFlags.INRANGE | PointerFlags.INCONTACT | PointerFlags.DOWN;
    
    InjectTouchInput(1, new[] { pointer });
    
    //Hold touch for some time
    Thread.Sleep(100);
    
    pointer.PointerInfo.PointerFlags = PointerFlags.UPDATE;
    
    InjectTouchInput(1, new []{ pointer });