2013-04-08 43 views
1

我正在嘗試使用python for windows 8編寫一種驅動程序。我將通過串行接收觸摸座標,然後使用它使其看起來好像有人觸摸了那些座標在屏幕上(模擬觸摸)。在Windows 8中使用Python模擬觸摸事件

我的第一個問題是:

是Windows 8的觸摸事件從僅僅在區域點擊鼠標有什麼不同? (我知道他們修補家居觸摸事件,但我不確定是什麼,涉及 - 可能喜歡的東西效果的區域,使其更觸摸屏友好的?)

其次:

如果是這樣,有沒有python庫來模擬一個'觸摸'在座標,而不是'點擊'?

UPDATE:

使用的ctypes和評論的鏈接頁面,我創造了這個:

from ctypes import * 

#Constants 

#For touchMask 
TOUCH_MASK_NONE=   0x00000000 #Default 
TOUCH_MASK_CONTACTAREA= 0x00000001 
TOUCH_MASK_ORIENTATION= 0x00000002 
TOUCH_MASK_PRESSURE=  0x00000004 
TOUCH_MASK_ALL=   0x00000007 

#For touchFlag 
TOUCH_FLAG_NONE=   0x00000000 

#For pointerType 
PT_POINTER=    0x00000001#All 
PT_TOUCH=     0x00000002 
PT_PEN=     0x00000003 
PT_MOUSE=     0x00000004 

#For pointerFlags 
POINTER_FLAG_NONE=  0x00000000#Default 
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_FOURTHBUTTON=0x00000080 
POINTER_FLAG_FIFTHBUTTON= 0x00000100 
POINTER_FLAG_PRIMARY=  0x00002000 
POINTER_FLAG_CONFIDENCE= 0x00004000 
POINTER_FLAG_CANCELED= 0x00008000 
POINTER_FLAG_DOWN=  0x00010000 
POINTER_FLAG_UPDATE=  0x00020000 
POINTER_FLAG_UP=   0x00040000 
POINTER_FLAG_WHEEL=  0x00080000 
POINTER_FLAG_HWHEEL=  0x00100000 
POINTER_FLAG_CAPTURECHANGED=0x00200000 


#Structs Needed 

class POINT(Structure): 
    _fields_=[("x", c_long), 
       ("y", c_long)] 

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

class RECT(Structure): 
    _fields_=[("left",c_long), 
       ("top",c_long), 
       ("right",c_long), 
       ("bottom",c_long)] 

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)] 



#Initialize Touch Injection 

pointerInfo=POINTER_INFO(pointerType=PT_TOUCH, 
         pointerId=0, 
         ptPixelLocation=POINT(950,540)) 

touchInfo=POINTER_TOUCH_INFO(pointerInfo=pointerInfo, 
          touchFlags=TOUCH_FLAG_NONE, 
          touchMask=TOUCH_MASK_ALL, 
          rcContact=RECT(pointerInfo.ptPixelLocation.x-5, 
            pointerInfo.ptPixelLocation.y-5, 
            pointerInfo.ptPixelLocation.x+5, 
            pointerInfo.ptPixelLocation.y+5), 
          orientation=90, 
          pressure=32000) 


if (windll.user32.InitializeTouchInjection(1,1) != 0): 
    print "Initialized Touch Injection" 
#Press Down 
touchInfo.pointerInfo.pointerFlags=(POINTER_FLAG_DOWN| 
            POINTER_FLAG_INRANGE| 
            POINTER_FLAG_INCONTACT) 
if (windll.user32.InjectTouchInput(1, byref(touchInfo))==0): 
    print "Failed with Error: "+ FormatError() 

else: 
    print "Touch Down Succeeded!" 

#Pull Up 
touchInfo.pointerInfo.pointerFlags=POINTER_FLAG_UP 
if (windll.user32.InjectTouchInput(1,byref(touchInfo))==0): 
    print "Failed with Error: "+FormatError() 

else: 
    print "Pull Up Succeeded!" 

每次失敗有關的輸入參數錯誤。 我已經經歷了每一個參考,並且找不到一個看起來不正確的類型。有沒有人看到明顯的東西?

感謝

+2

你檢查[這](http://stackoverflow.com/questions/7507568/is-there-a -way-to-simulate-touch-events-in-windows-8?rq = 1) – Xyroid 2013-04-08 12:17:20

+0

我還沒有看到。謝謝。那麼有可能使用ctypes來獲得這個與Python的工作?對不起,我完全不熟悉包裝或它們的工作方式。 – Legault 2013-04-08 17:12:44

+0

我也不確定,因爲我不熟悉C++或Python。我完全是一個C#人:)你正在開發Windows商店應用程序或Windows 8桌面應用程序? – Xyroid 2013-04-08 17:19:18

回答

1

感謝Eryksum和Xyroid,我能得到它的工作。感謝您支持我的C型/ Windows無知。下面是打包成一個功能的觸摸仿真的最終腳本(額外的常數以及):

from ctypes import * 
from ctypes.wintypes import * 

#Constants 

#For touchMask 
TOUCH_MASK_NONE=   0x00000000 #Default 
TOUCH_MASK_CONTACTAREA= 0x00000001 
TOUCH_MASK_ORIENTATION= 0x00000002 
TOUCH_MASK_PRESSURE=  0x00000004 
TOUCH_MASK_ALL=   0x00000007 

#For touchFlag 
TOUCH_FLAG_NONE=   0x00000000 

#For pointerType 
PT_POINTER=    0x00000001#All 
PT_TOUCH=     0x00000002 
PT_PEN=     0x00000003 
PT_MOUSE=     0x00000004 

#For pointerFlags 
POINTER_FLAG_NONE=  0x00000000#Default 
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_FOURTHBUTTON=0x00000080 
POINTER_FLAG_FIFTHBUTTON= 0x00000100 
POINTER_FLAG_PRIMARY=  0x00002000 
POINTER_FLAG_CONFIDENCE= 0x00004000 
POINTER_FLAG_CANCELED= 0x00008000 
POINTER_FLAG_DOWN=  0x00010000 
POINTER_FLAG_UPDATE=  0x00020000 
POINTER_FLAG_UP=   0x00040000 
POINTER_FLAG_WHEEL=  0x00080000 
POINTER_FLAG_HWHEEL=  0x00100000 
POINTER_FLAG_CAPTURECHANGED=0x00200000 


#Structs Needed 

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)] 



#Initialize Pointer and Touch info 

pointerInfo=POINTER_INFO(pointerType=PT_TOUCH, 
         pointerId=0, 
         ptPixelLocation=POINT(950,540)) 

touchInfo=POINTER_TOUCH_INFO(pointerInfo=pointerInfo, 
          touchFlags=TOUCH_FLAG_NONE, 
          touchMask=TOUCH_MASK_ALL, 
          rcContact=RECT(pointerInfo.ptPixelLocation.x-5, 
            pointerInfo.ptPixelLocation.y-5, 
            pointerInfo.ptPixelLocation.x+5, 
            pointerInfo.ptPixelLocation.y+5), 
          orientation=90, 
          pressure=32000) 


def makeTouch(x,y,fingerRadius): 
    touchInfo.pointerInfo.ptPixelLocation.x=x 
    touchInfo.pointerInfo.ptPixelLocation.y=y 

    touchInfo.rcContact.left=x-fingerRadius 
    touchInfo.rcContact.right=x+fingerRadius 
    touchInfo.rcContact.top=y-fingerRadius 
    touchInfo.rcContact.bottom=y+fingerRadius 

    #Initialize Touch Injection 
    if (windll.user32.InitializeTouchInjection(1,1) != 0): 
     print "Initialized Touch Injection" 

    #Press Down 
    touchInfo.pointerInfo.pointerFlags=(POINTER_FLAG_DOWN| 
             POINTER_FLAG_INRANGE| 
             POINTER_FLAG_INCONTACT) 

    if (windll.user32.InjectTouchInput(1, byref(touchInfo))==0): 
     print "Failed with Error: "+ FormatError() 

    else: 
     print "Touch Down Succeeded!" 

    #Pull Up 
    touchInfo.pointerInfo.pointerFlags=POINTER_FLAG_UP 

    if (windll.user32.InjectTouchInput(1,byref(touchInfo))==0): 
     print "Failed with Error: "+FormatError() 

    else: 
     print "Pull Up Succeeded!" 

    return 

#Ex: 
#makeTouch(950,270,5)