Search code examples
c#windows-8touchgesturehardware-interface

Windows 8 - Simulate gesture on resistive Touch Screen using c#


Is there any way to simulate Windows 8 gesture (zooming, rotation, swipe) on a resistive touch screen device using a physical keyboard key mapped with C# interface ?

My setup is an classic Desktop Windows 8 application destinated to 2 screen types (resistive and capacitive touch screen). On resistive screen, multi touch gestures are not recognized and can be done using keyboard combination or a mapped key (no mouse attached on this device).


Solution

  • Windows 8 provides native touch injection functions InitializeTouchInjection and InjectTouchInput that you can p-invoke from C# to synthesize pointer input that can translate to gestures.

    The Input: Touch injection sample demonstrates this in C++.

    p-invoke signatures generated by the PInvoke Interop Assistant

    public partial class NativeConstants {
    
        /// TOUCH_FLAG_NONE -> 0x00000000
        public const int TOUCH_FLAG_NONE = 0;
    
        /// TOUCH_MASK_NONE -> 0x00000000
        public const int TOUCH_MASK_NONE = 0;
    
        /// TOUCH_MASK_CONTACTAREA -> 0x00000001
        public const int TOUCH_MASK_CONTACTAREA = 1;
    
        /// TOUCH_MASK_ORIENTATION -> 0x00000002
        public const int TOUCH_MASK_ORIENTATION = 2;
    
        /// TOUCH_MASK_PRESSURE -> 0x00000004
        public const int TOUCH_MASK_PRESSURE = 4;
    
        /// POINTER_FLAG_NONE -> 0x00000000
        public const int POINTER_FLAG_NONE = 0;
    
        /// POINTER_FLAG_NEW -> 0x00000001
        public const int POINTER_FLAG_NEW = 1;
    
        /// POINTER_FLAG_INRANGE -> 0x00000002
        public const int POINTER_FLAG_INRANGE = 2;
    
        /// POINTER_FLAG_INCONTACT -> 0x00000004
        public const int POINTER_FLAG_INCONTACT = 4;
    
        /// POINTER_FLAG_FIRSTBUTTON -> 0x00000010
        public const int POINTER_FLAG_FIRSTBUTTON = 16;
    
        /// POINTER_FLAG_SECONDBUTTON -> 0x00000020
        public const int POINTER_FLAG_SECONDBUTTON = 32;
    
        /// POINTER_FLAG_THIRDBUTTON -> 0x00000040
        public const int POINTER_FLAG_THIRDBUTTON = 64;
    
        /// POINTER_FLAG_FOURTHBUTTON -> 0x00000080
        public const int POINTER_FLAG_FOURTHBUTTON = 128;
    
        /// POINTER_FLAG_FIFTHBUTTON -> 0x00000100
        public const int POINTER_FLAG_FIFTHBUTTON = 256;
    
        /// POINTER_FLAG_PRIMARY -> 0x00002000
        public const int POINTER_FLAG_PRIMARY = 8192;
    
        /// POINTER_FLAG_CONFIDENCE -> 0x00004000
        public const int POINTER_FLAG_CONFIDENCE = 16384;
    
        /// POINTER_FLAG_CANCELED -> 0x00008000
        public const int POINTER_FLAG_CANCELED = 32768;
    
        /// POINTER_FLAG_DOWN -> 0x00010000
        public const int POINTER_FLAG_DOWN = 65536;
    
        /// POINTER_FLAG_UPDATE -> 0x00020000
        public const int POINTER_FLAG_UPDATE = 131072;
    
        /// POINTER_FLAG_UP -> 0x00040000
        public const int POINTER_FLAG_UP = 262144;
    
        /// POINTER_FLAG_WHEEL -> 0x00080000
        public const int POINTER_FLAG_WHEEL = 524288;
    
        /// POINTER_FLAG_HWHEEL -> 0x00100000
        public const int POINTER_FLAG_HWHEEL = 1048576;
    
        /// POINTER_FLAG_CAPTURECHANGED -> 0x00200000
        public const int POINTER_FLAG_CAPTURECHANGED = 2097152;
    
        /// POINTER_FLAG_HASTRANSFORM -> 0x00400000
        public const int POINTER_FLAG_HASTRANSFORM = 4194304;
    }
    
    public enum tagPOINTER_INPUT_TYPE {
    
        /// PT_POINTER -> 0x00000001
        PT_POINTER = 1,
    
        /// PT_TOUCH -> 0x00000002
        PT_TOUCH = 2,
    
        /// PT_PEN -> 0x00000003
        PT_PEN = 3,
    
        /// PT_MOUSE -> 0x00000004
        PT_MOUSE = 4,
    
        /// PT_TOUCHPAD -> 0x00000005
        PT_TOUCHPAD = 5,
    }
    
    public enum tagPOINTER_BUTTON_CHANGE_TYPE {
    
        POINTER_CHANGE_NONE,
    
        POINTER_CHANGE_FIRSTBUTTON_DOWN,
    
        POINTER_CHANGE_FIRSTBUTTON_UP,
    
        POINTER_CHANGE_SECONDBUTTON_DOWN,
    
        POINTER_CHANGE_SECONDBUTTON_UP,
    
        POINTER_CHANGE_THIRDBUTTON_DOWN,
    
        POINTER_CHANGE_THIRDBUTTON_UP,
    
        POINTER_CHANGE_FOURTHBUTTON_DOWN,
    
        POINTER_CHANGE_FOURTHBUTTON_UP,
    
        POINTER_CHANGE_FIFTHBUTTON_DOWN,
    
        POINTER_CHANGE_FIFTHBUTTON_UP,
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct tagPOINTER_INFO {
    
        /// POINTER_INPUT_TYPE->DWORD->unsigned int
        public uint pointerType;
    
        /// UINT32->unsigned int
        public uint pointerId;
    
        /// UINT32->unsigned int
        public uint frameId;
    
        /// POINTER_FLAGS->UINT32->unsigned int
        public uint pointerFlags;
    
        /// HANDLE->void*
        public System.IntPtr sourceDevice;
    
        /// HWND->HWND__*
        public System.IntPtr hwndTarget;
    
        /// POINT->tagPOINT
        public Point ptPixelLocation;
    
        /// POINT->tagPOINT
        public Point ptHimetricLocation;
    
        /// POINT->tagPOINT
        public Point ptPixelLocationRaw;
    
        /// POINT->tagPOINT
        public Point ptHimetricLocationRaw;
    
        /// DWORD->unsigned int
        public uint dwTime;
    
        /// UINT32->unsigned int
        public uint historyCount;
    
        /// INT32->int
        public int inputData;
    
        /// DWORD->unsigned int
        public uint dwKeyStates;
    
        /// UINT64->unsigned __int64
        public ulong PerformanceCount;
    
        /// POINTER_BUTTON_CHANGE_TYPE->tagPOINTER_BUTTON_CHANGE_TYPE
        public tagPOINTER_BUTTON_CHANGE_TYPE ButtonChangeType;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct tagPOINTER_TOUCH_INFO {
    
        /// POINTER_INFO->tagPOINTER_INFO
        public tagPOINTER_INFO pointerInfo;
    
        /// TOUCH_FLAGS->UINT32->unsigned int
        public uint touchFlags;
    
        /// TOUCH_MASK->UINT32->unsigned int
        public uint touchMask;
    
        /// RECT->tagRECT
        public tagRECT rcContact;
    
        /// RECT->tagRECT
        public tagRECT rcContactRaw;
    
        /// UINT32->unsigned int
        public uint orientation;
    
        /// UINT32->unsigned int
        public uint pressure;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct Point {
    
        /// LONG->int
        public int x;
    
        /// LONG->int
        public int y;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct tagRECT {
    
        /// LONG->int
        public int left;
    
        /// LONG->int
        public int top;
    
        /// LONG->int
        public int right;
    
        /// LONG->int
        public int bottom;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct HWND__ {
    
        /// int
        public int unused;
    }
    
    public partial class NativeMethods {
    
        /// Return Type: BOOL->int
        ///maxCount: UINT32->unsigned int
        ///dwMode: DWORD->unsigned int
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint="InitializeTouchInjection")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public static extern  bool InitializeTouchInjection(uint maxCount, uint dwMode) ;
    
    
        /// Return Type: BOOL->int
        ///count: UINT32->unsigned int
        ///contacts: POINTER_TOUCH_INFO*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint="InjectTouchInput")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public static extern  bool InjectTouchInput(uint count, ref tagPOINTER_TOUCH_INFO contacts) ;
    
    }