如何在python中使用InjectTouchInput进行多点触摸

时间:2018-10-27 05:01:37

标签: python touch converters

我正在尝试使用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。

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

1 个答案:

答案 0 :(得分:0)

我通过将tinfo的定义更改为解决了该问题:

tinfo = (POINTER_TOUCH_INFO * ntouch)()

此链接很有帮助:Passing an array of struct from Python to dll