首页 > 解决方案 > 如何在 python 中使用 InjectTouchInput 进行多点触控

问题描述

我正在尝试使用 InjectTouchInput 来模拟屏幕上的几次触摸。

当我只有一次触摸时一切都很好,但对于不止一种情况,我被困在类型转换问题上

这是测试代码:

class POINTER_INFO(Structure):
_fields_=[("pointerType",c_uint32),
          ("pointerId",c_uint32),
          ("frameId",c_uint32),
          ("pointerFlags",c_int),
          ("sourceDevice",HANDLE),
          ("hwndTarget",HWND),
          ("ptPixelLocation",POINT),
          ("ptHimetricLocation",POINT),
          ("ptPixelLocationRaw",POINT),
          ("ptHimetricLocationRaw",POINT),
          ("dwTime",DWORD),
          ("historyCount",c_uint32),
          ("inputData",c_int32),
          ("dwKeyStates",DWORD),
          ("PerformanceCount",c_uint64),
          ("ButtonChangeType",c_int)
          ]

class POINTER_TOUCH_INFO(Structure):
_fields_=[("pointerInfo",POINTER_INFO),
          ("touchFlags",c_int),
          ("touchMask",c_int),
          ("rcContact", RECT),
          ("rcContactRaw",RECT),
          ("orientation", c_uint32),
          ("pressure", c_uint32)]


ntouch=2

if (windll.user32.InitializeTouchInjection(ntouch,TOUCH_FEEDBACK_INDIRECT) ==  False): 
    print ("Initialized Touch Injection Error")

tinfo = [POINTER_TOUCH_INFO() for i in range(ntouch)]

tinfo[0].pointerInfo.pointerType = PT_TOUCH
tinfo[0].pointerInfo.pointerId = 0
tinfo[0].pointerInfo.ptPixelLocation.y = 1000
tinfo[0].pointerInfo.ptPixelLocation.x = 500

tinfo[0].touchFlags = TOUCH_FLAG_NONE
tinfo[0].touchMask = TOUCH_MASK_ALL
tinfo[0].orientation = 90;
tinfo[0].pressure = 32000;
tinfo[0].rcContact.top = tinfo[0].pointerInfo.ptPixelLocation.y - 2;
tinfo[0].rcContact.bottom = tinfo[0].pointerInfo.ptPixelLocation.y + 2;
tinfo[0].rcContact.left = tinfo[0].pointerInfo.ptPixelLocation.x  - 2;
tinfo[0].rcContact.right = tinfo[0].pointerInfo.ptPixelLocation.x  + 2;

tinfo[0].pointerInfo.pointerFlags=POINTER_FLAG_DOWN | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT)

tinfo[1].pointerInfo.pointerType = PT_TOUCH
tinfo[1].pointerInfo.pointerId = 1
tinfo[1].pointerInfo.ptPixelLocation.y = 900
tinfo[1].pointerInfo.ptPixelLocation.x = 300

tinfo[1].touchFlags = TOUCH_FLAG_NONE
tinfo[1].touchMask = TOUCH_MASK_ALL
tinfo[1].orientation = 90;
tinfo[1].pressure = 32000;
tinfo[1].rcContact.top = tinfo[1].pointerInfo.ptPixelLocation.y - 2;
tinfo[1].rcContact.bottom = tinfo[1].pointerInfo.ptPixelLocation.y + 2;
tinfo[1].rcContact.left = tinfo[1].pointerInfo.ptPixelLocation.x  - 2;
tinfo[1].rcContact.right = tinfo[1].pointerInfo.ptPixelLocation.x  + 2;

tinfo[1].pointerInfo.pointerFlags=(POINTER_FLAG_DOWN | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT)

if (windll.user32.InjectTouchInput(ntouch, ctypes.byref(tinfo))==0):    
    print (" Error: "+ ctypes.FormatError())

InjectTouchInput 函数返回以下错误:

TypeError:byref() 参数必须是 ctypes 实例,而不是“列表”

我已经尝试过一些转换,因为它传递了以下结果:

tinfop=(ctypes.Structure*ntouch)(*tinfo)

但在这种情况下,它返回:

TypeError: _type_ 必须有存储信息

如果 ntouch=1 (单点触摸),则代码可以正常工作。

我能找到的例子只有 c++ ( https://stackoverflow.com/questions/20875283/why-is-only-one-touch-being-injected-when-using-touch-injection-api-with -win8或单点触控 python。

基本上我认为我正在寻找一种将结构列表转换为指向其第一项的指针的方法。

标签: pythontouchconverters

解决方案


我通过将 tinfo 的定义更改为:

tinfo = (POINTER_TOUCH_INFO * ntouch)()

这个链接很有帮助:Passing a array of struct from Python to dll


推荐阅读