simulate windows touch input

如何模拟windows的touch操作,奋斗了几天终于出结果了,网上搜索了一个c#版本的,但是总是没有效果,并且c#要求的条件比较多,于是自己写了一个autoit轻量级的版本。autoit代码:

#RequireAdmin
Local $retval = _SimulateTouch(10, 1000, 300, 300)
MsgBox(0, "", $retval)
; #INDEX# =======================================================================================================================
; Title .........: _SimulateTouch
; AutoIt Version : 3.3.6
; Description ...: Simulate touch action. The function can work on minimum supported client: windows8[desktop apps only] and minimum supported server: windows server 2012[desktop apps only]
; Author(s) .....: [email protected]163.com
; Dll(s) ........: user32.dll
; Parameters.....: $TouchTimes-----------Touch times;
;                   $TouchInterval--------Touch Interval;
;                   $TouchCoordinates_x---Touch coordinates x;
;                   $TouchCoordinates_y---Touch coordinates y
; returns........: int  touch success times
; Requirements ..:
; ===============================================================================================================================
Func _SimulateTouch($TouchTimes, $TouchInterval, $TouchCoordinates_x, $TouchCoordinates_y)
    Local $intRetVal = 0    ;record the touch success time
    If Not _IsConsistTouch() Then        ;check if the pc support touch action
        Return $intRetVal
    EndIf
    Local $int_Touch_Count_MAX = 10    ;the fingercount
    Local $TOUCH_FEEDBACK_DEFAULT = 1

    $TouchInit = DllCall("User32.dll", "BOOL", "InitializeTouchInjection", "int", $int_Touch_Count_MAX, "DWORD", $TOUCH_FEEDBACK_DEFAULT)

    Local $TOUCH_MASK_CONTACTAREA = 0x00000001
    Local $TOUCH_MASK_ORIENTATION = 0x00000002
    Local $TOUCH_MASK_PRESSURE = 0x00000004

    Local $POINTER_FLAG_UP = 0x00040000
    Local $POINTER_FLAG_DOWN = 0x00010000
    Local $POINTER_FLAG_INRANGE = 0x00000002
    Local $POINTER_FLAG_INCONTACT = 0x00000004
    Local $POINTER_FLAG_UPDATE = 0X00020000

    Local $tag_POINTER_INPUT_TYPE = 0x00000002
    Local $tag_POINTER_FLAGS = BitOR($POINTER_FLAG_DOWN, $POINTER_FLAG_INRANGE , $POINTER_FLAG_INCONTACT)
    Local $tag_TOUCH_FLAGS_NONE = 0x00000000
    Local $TOUCH_MASK = BitOR($TOUCH_MASK_CONTACTAREA , $TOUCH_MASK_ORIENTATION , $TOUCH_MASK_PRESSURE)
    Local $POINT_TOUCH_TYPE = 0x00000002    ;PT_TOUCH

    Local $ptPixelLocation_x = $TouchCoordinates_x
    Local $ptPixelLocation_y = $TouchCoordinates_y

    Local $tag_POINTER_INFO = "struct;struct;int pointerType;" & _
                                    "int pointerId;" & _
                                    "int frameId;" & _
                                    "int pointerFlags;" & _
                                    "hwnd sourceDevice;" & _
                                    "hwnd hwndTarget;" & _
                                    "struct;int ptPixelLocation_x;" & _
                                    "int ptPixelLocation_y;endstruct;" & _
                                    "struct;int ptHimetricLocation_x;" & _
                                    "int ptHimetricLocation_y;endstruct;" & _
                                    "struct;int ptPixelLocationRaw_x;" & _
                                    "int ptPixelLocationRaw_y;endstruct;" & _
                                    "struct;int ptHimetricLocationRaw_x;" & _
                                    "int ptHimetricLocationRaw_y;endstruct;" & _
                                    "dword dwTime;" & _
                                    "int historyCount;" & _
                                    "int inputData;" & _
                                    "dword dwKeyStates;" & _
                                    "UINT64 PerformanceCount;" & _
                                    "int ButtonchangeType;endstruct;"
    Local Const $POINTER_TOUCH_INFO = $tag_POINTER_INFO & "int touchFlags;" & _
                                                    "int touchMask;" & _
                                                    "struct;int rcContact_left;" & _
                                                    "int rcContact_right;" & _
                                                    "int rcContact_top;" & _
                                                    "int rcContact_bottom;endstruct;" & _
                                                    "struct;int rcContactRaw_left;" & _
                                                    "int rcContactRaw_right;" & _
                                                    "int rcContactRaw_top;" & _
                                                    "int rcContactRaw_bottom;endstruct;" & _
                                                    "int orientation;" & _
                                                    "int pressure;endstruct;"

    Local $t_POINTER_TOUCH_INFO = DllStructCreate($POINTER_TOUCH_INFO)

    DllStructSetData($t_POINTER_TOUCH_INFO, "pointerType", $POINT_TOUCH_TYPE)    ;pointerType
    DllStructSetData($t_POINTER_TOUCH_INFO, "pointerId", 0)    ;pointerId

    DllStructSetData($t_POINTER_TOUCH_INFO, "ptPixelLocation_x", $ptPixelLocation_x)    ;ptPixelLocation
    DllStructSetData($t_POINTER_TOUCH_INFO, "ptPixelLocation_y", $ptPixelLocation_y)    ;ptPixelLocation

    DllStructSetData($t_POINTER_TOUCH_INFO, "touchFlags", $tag_TOUCH_FLAGS_NONE)    ;touchFlags
    DllStructSetData($t_POINTER_TOUCH_INFO, "touchMask", $TOUCH_MASK)    ;touchMask
    DllStructSetData($t_POINTER_TOUCH_INFO, "rcContact_left", $ptPixelLocation_x - 2)    ;rcContact
    DllStructSetData($t_POINTER_TOUCH_INFO, "rcContact_right", $ptPixelLocation_x + 2)    ;rcContact
    DllStructSetData($t_POINTER_TOUCH_INFO, "rcContact_top", $ptPixelLocation_y - 2)    ;rcContact
    DllStructSetData($t_POINTER_TOUCH_INFO, "rcContact_bottom", $ptPixelLocation_y + 2)    ;rcContact

    DllStructSetData($t_POINTER_TOUCH_INFO, "orientation", 90)    ;orientation
    DllStructSetData($t_POINTER_TOUCH_INFO, "pressure", 32000)    ;pressure

    For $i = 1 To $TouchTimes Step 1
        DllStructSetData($t_POINTER_TOUCH_INFO, "pointerFlags", $tag_POINTER_FLAGS)    ;pinterFlags
        ;DllStructSetData($t_POINTER_TOUCH_INFO, "ptPixelLocation_y", 300-$i)    ;ptPixelLocation draw a line
        $aRet = DllCall(‘User32.dll‘, ‘BOOL‘, ‘InjectTouchInput‘, ‘int‘, 1, ‘ptr‘, DllStructGetPtr($t_POINTER_TOUCH_INFO))

        If $aRet <> 0 Then
            DllStructSetData($t_POINTER_TOUCH_INFO, "pointerFlags", $POINTER_FLAG_UP)    ;pinterFlags
            DllCall(‘User32.dll‘, ‘BOOL‘, ‘InjectTouchInput‘, ‘int‘, 1, ‘ptr‘, DllStructGetPtr($t_POINTER_TOUCH_INFO))
            $intRetVal += 1
            Sleep($TouchInterval)
        EndIf
    Next
    Return $intRetVal
EndFunc

;check if the pc support touch action
;returns:1----yes;0----no
Func _IsConsistTouch()
    Dim $IsConsistTouch = GetSystemMetrics(94)    ;94 means SM_DIGITIZER
    Return $IsConsistTouch > 0 ? 1 : 0
EndFunc

Func GetSystemMetrics($iIndex)
    Local $aResult = DllCall("user32.dll", "int", "GetSystemMetrics", "int", $iIndex)
    If @error Then Return SetError(@error, @extended, 0)

    Return $aResult[0]
EndFunc   ;==>_WinAPI_GetSystemMetrics

C#代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{

    [StructLayout(LayoutKind.Explicit)]
    public struct Rect
    {
        [FieldOffset(0)]
        public int left;
        [FieldOffset(4)]
        public int top;
        [FieldOffset(8)]
        public int right;
        [FieldOffset(12)]
        public int bottom;
    }

    public enum TOUCH_FLAGS { TOUCH_FLAGS_NONE = 0x00000000/*Indicates that no flags are set.*/ }

    public enum POINTER_FLAGS
    {
        POINTER_FLAG_NONE = 0x00000000,
        POINTER_FLAG_NEW = 0x00000001,
        POINTER_FLAG_INRANGE = 0x00000002,
        POINTER_FLAG_INCONTACT = 0x00000004,
        POINTER_FLAG_FIRSTBUTTON = 0x00000010,
        POINTER_FLAG_SECONDBUTTON = 0x00000020,
        POINTER_FLAG_THIRDBUTTON = 0x00000040,
        POINTER_FLAG_OTHERBUTTON = 0x00000080,
        POINTER_FLAG_PRIMARY = 0x00000100,
        POINTER_FLAG_CONFIDENCE = 0x00000200,
        POINTER_FLAG_CANCELLED = 0x00000400,
        POINTER_FLAG_DOWN = 0x00010000,
        POINTER_FLAG_UPDATE = 0x00020000,
        POINTER_FLAG_UP = 0x00040000,
        POINTER_FLAG_WHEEL = 0x00080000,
        POINTER_FLAG_HWHEEL = 0x00100000
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
    }

    public enum POINTER_INPUT_TYPE
    {
        PT_POINTER = 0x00000001,
        PT_TOUCH = 0x00000002,
        PT_PEN = 0x00000003,
        PT_MOUSE = 0x00000004
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct POINTER_INFO
    {
        public POINTER_INPUT_TYPE pointerType;
        public uint pointerId;
        public uint frameId;
        public IntPtr sourceDevice;
        public IntPtr hwndTarget;
        public POINT ptPixelLocation;
        public POINT ptHimetricLocation;
        public POINT ptPixelLocationPredicted;
        public POINT ptHimetricLocationPredicted;
        public POINTER_FLAGS pointerFlags;
        public uint dwTime;
        public uint historyCount;
        public uint inputData;
        public uint dwKeyStates;
        public ulong Reserved;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct POINTER_TOUCH_INFO
    {
        /*
         * Contains basic pointer information common to all pointer types.
         */
        public POINTER_INFO pointerInfo;

        /*
         * Lists the touch flags.
         */
        public TOUCH_FLAGS touchFlags;
        public uint touchMasks;
        /*
         * Pointer contact area in pixel screen coordinates.
         * By default, if the device does not report a contact area,
         * this field defaults to a 0-by-0 rectangle centered around the pointer location.
         */
        public Rect rcContact;

        /*
         * A pointer orientation, with a value between 0 and 359, where 0 indicates a touch pointer
         * aligned with the x-axis and pointing from left to right; increasing values indicate degrees
         * of rotation in the clockwise direction.
         * This field defaults to 0 if the device does not report orientation.
         */
        public uint orientation;

        /*
         * Pointer pressure normalized in a range of 0 to 256.
         */
        public uint pressure;
    }

    public partial class Form1 : Form
    {
        const int MAX_TOUCH_COUNT = 256;

        //Specifies default touch visualizations.
        const int TOUCH_FEEDBACK_DEFAULT = 0x1;

        //Specifies indirect touch visualizations.
        const int TOUCH_FEEDBACK_INDIRECT = 0x2;

        //Specifies no touch visualizations.
        const int TOUCH_FEEDBACK_NONE = 0x3;

        [DllImport("User32.dll")]
        static extern bool InitializeTouchInjection(uint maxCount, int dwMode);

        [DllImport("User32.dll")]
        static extern bool InjectTouchInput(int count, ref POINTER_TOUCH_INFO contacts);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool flag = false;
            flag = InitializeTouchInjection(10, TOUCH_FEEDBACK_NONE);

            POINTER_TOUCH_INFO contacts = new POINTER_TOUCH_INFO();

            Rect touchArea = new Rect();
            touchArea.left = 200 - 2;
            touchArea.right = 200 + 2;
            touchArea.top = 200 + 2;
            touchArea.bottom = 200 - 2;

            contacts.pointerInfo.pointerType = POINTER_INPUT_TYPE.PT_TOUCH;
            contacts.touchFlags = TOUCH_FLAGS.TOUCH_FLAGS_NONE;
            contacts.rcContact = touchArea;
            contacts.orientation = 90;
            contacts.pressure = 32000;
            contacts.pointerInfo.pointerFlags = POINTER_FLAGS.POINTER_FLAG_UP | POINTER_FLAGS.POINTER_FLAG_INRANGE | POINTER_FLAGS.POINTER_FLAG_INCONTACT;
            contacts.pointerInfo.ptPixelLocation.x = 200;
            contacts.pointerInfo.ptPixelLocation.y = 200;
            contacts.pointerInfo.pointerId = 1;

            flag = InjectTouchInput(1, ref contacts);//returned flag is false which means the injection fails!

            contacts.pressure = 0;
            if (flag)
            {
                contacts.pointerInfo.pointerFlags = POINTER_FLAGS.POINTER_FLAG_UP;
                InjectTouchInput(1, ref contacts);
            }
        }
    }
}
时间: 2024-10-07 12:06:37

simulate windows touch input的相关文章

Windows Touch Input WM GESTURE WM TOUCH

分享一下我老师大神的人工智能教程吧.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net PS:如果提示WM_TOUCH未定义,那么可以考虑使用消息WM_TOUCHMOVE代替.另关于消息WM_TOUCHDOWN.WM_TOUCHUP两个消息,Windows7操作系统目前只是作了定义,但未响应...#ifndef WM_TOUCH      // 自定义多点触摸消息#define WM_TOUCH 0x0240#endi

Advanced Touch Input(高级触摸输入)

金田 今天要讲的主题是iOS 9高级触摸输入,更准确地讲,是在iOS9上如何减少触摸输入到屏幕显示的延迟程度,此次将分 低延迟渲染(iOS9 渲染性能优化)和 触摸点方案改进 两个方面来介绍. 低延迟渲染 1)介绍 一直以来,iOS 里面的触摸手势延迟(latency)是一个极为影响用户体验,比较头痛,却又比较棘手的一个问题.试想我们画一条线,手势稍快一点就会是手指在前面跑,画的线在后面追. 图1.1 手势延迟示意图(截取自WWDC演示文档) 值得我们关注的是,此次发布的iOS 9操作系统,专门

windows C input 注意

windows控制台输入,默认是以文本模式打开,即使重定向输入,文本模式不变,所以输入时无法读到cr,因为windows已经把crlf转换成单个lf. 如果freopen("CON", "rb",stdin); 则可以读到cr fgets会吸收换行,但是换行也被写入字符串 如果不要字符串,scanf("%d ",&d); gets()最好.vs2015删除gets,用gets_s即可. 首先:fgets读入的是带'\n'的字符串.也就是说

simulate windows postmessage or keydown

2 ways: 1. under TForm:   if assigned(focused) then      Focused.keydown(key,keychar,[]); 2. using screen object: if assigned(Screen.focusControl) then      Screen.focusControl.keydown(key,keychar,[]); I think lots of people want to know this. Best r

2019-11-29-WPF-客户端开发需要知道的触摸失效问题

原文:2019-11-29-WPF-客户端开发需要知道的触摸失效问题 title author date CreateTime categories WPF 客户端开发需要知道的触摸失效问题 lindexi 2019-11-29 8:50:2 +0800 2019-9-18 10:3:57 +0800 WPF 在说 WPF 开发的时候,如果开发的应用是触摸应用,那么熟悉的小伙伴会说到触摸失效问题.从分类上触摸失效有系统的触摸失效,应用程序的触摸失效,同时触摸失效指的不是触摸没反应,本文详细告诉大

背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板

[源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) 通过处理 Pointer 相关事件实现一个简单的涂鸦板 InkCanvas 基础知识 示例1.演示如何通过 Pointer 相关事件的处理,来实现一个简单的涂鸦板Controls/MediaControl/InkSimple.xaml <Page x:Class="Windows10.Controls.MediaControl.InkSimple" xmlns="http://s

转载:Windows 8 输入: 边缘手势, 手势操作, 手势识别

重新想象 Windows 8 Store Apps (50) - 输入: 边缘手势, 手势操作, 手势识别 作者:webabcd 介绍 重新想象 Windows 8 Store Apps 之 手势 监测边缘手势 手势操作 - Manipulate 的应用(位移手势,缩放手势,旋转手势) 手势识别 - GestureRecognizer 的应用 示例 1.演示如何监测边缘手势 Input/Touch/EdgeGestureDemo.xaml <Page x:Class="XamlDemo.I

背水一战 Windows 10 (8) - 控件 UI: StateTrigger

[源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件 UI VisualState 之 StateTrigger 示例1.自定义 StateTriggerControls/UI/VisualState/MyDeviceFamilyStateTrigger.cs /* * 用于演示自定义 StateTrigger * * * StateTriggerBase - 自定义 StateTrigger 需要继承此基类 * SetActive(Boolean IsActive)

基于Visual C++2010与windows SDK fo windows7开发Windows 7的多点触摸特性应用

2008年5月28日,微软即将退休的精神领袖比尔·盖茨和首席执行官史蒂夫·鲍尔默共同在北圣地亚哥四季艾维亚拉洲际度假村举办的2008 D6 All Things数字化大会上向到场的嘉宾展示了windows7的测试版.     鲍尔默向记者打趣道:"今天只给你们看一丁点儿(just a snippet)哟!"然而就是那么一丁点儿,已经让世界为之惊艳! 盖茨和鲍尔默在D6 All Things数字化大会上重点展示的是多触点技术(multitouch technology),这是运行Wind