2016-08-30 736 views
0

我在使用PyAutoGUI函數庫,如何知道是否按下鼠標左鍵?PyAutoGUi:如何知道是否按下鼠標左鍵

這就是我想做的事:

if(leftmousebuttonpressed): 
    print("left") 
else: 
    print("nothing") 

幫助表示讚賞。

+1

是不是PyAutoGUI用來控制鼠標,而不是從中讀取?快速掃描他們的文檔顯示沒有onMouseClick事件,或任何類似的事件。 –

+0

有什麼我可以用來閱讀python中的mouseclick? – Coding4Life

+1

是的 - 查看PyUserInput,可能正是您需要的。 –

回答

1

我不認爲你可以使用PyAutoGui聽鼠標點擊。

而是嘗試Pyhook(從源頁):

import pythoncom, pyHook 

def OnMouseEvent(event): 
    # called when mouse events are received 
    print 'MessageName:',event.MessageName 
    print 'Message:',event.Message 
    print 'Time:',event.Time 
    print 'Window:',event.Window 
    print 'WindowName:',event.WindowName 
    print 'Position:',event.Position 
    print 'Wheel:',event.Wheel 
    print 'Injected:',event.Injected 
    print '---' 

# return True to pass the event to other handlers 
    return True 

# create a hook manager 
hm = pyHook.HookManager() 
# watch for all mouse events 
hm.MouseAll = OnMouseEvent 
# set the hook 
hm.HookMouse() 
# wait forever 
pythoncom.PumpMessages() 

我相信你可以這樣做:

import pyHook, pythoncom 

def left_down(): 
    print("left down") 

def right_down(): 
    print("right down") 

hm = pyHook.HookManager() 
hm.SubscribeMouseLeftDown(left_down) 
hm.SubscribeMouseRightDown(right_down) 
hm.HookMouse() 
pythoncom.PumpMessages() 
hm.UnhookMouse() 

他們還做鍵盤事件,只是去看看他們的API了。

編輯: 這裏是他們的小教程:https://sourceforge.net/p/pyhook/wiki/PyHook_Tutorial/

而且PyHook是僅適用於Windows(感謝李四指點出來)

+0

如果你提供一個鏈接到pyHook進行快速訪問,那麼這將是很好的,值得注意的是pyhook只能在windows上運行。 –

2

我可以證實,目前PyAutoGUI不能讀取/記錄點擊或擊鍵。這些功能在路線圖上,但目前沒有任何資源或時間表專用於它們。

相關問題