【问题标题】:How to use InjectTouchInput in python for multiple touch如何在 python 中使用 InjectTouchInput 进行多点触控
【发布时间】:2018-10-27 05:01:37
【问题描述】:

我正在尝试使用 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 实例,而不是 'list'

我已经尝试过一些强制转换作为传递结果:

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。

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

【问题讨论】:

    标签: python touch converters


    【解决方案1】:

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

    tinfo = (POINTER_TOUCH_INFO * ntouch)()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多