2016-03-08 182 views
0

我用Python編寫了一個小程序,當我按下鼠標左鍵時,它執行「go」功能。然而我的意圖是隻有在我持有LMB的時候纔去執行。如果我釋放LMB,它應該取消「去」。最後,當被取消或成功執行時,當我再次按下LMB時,應該做好相同的準備。PyHook鼠標事件

下面是代碼:

import pyHook 
import pythoncom 
import pyautogui 
from time import sleep 
pyautogui.size() 

width, height = pyautogui.size() 


def go(event): 

    sleep(0.099) 
    pyautogui.moveRel(4, 19, duration=0.099) 
    pyautogui.moveRel(-4, 7, duration=0.099) 
    pyautogui.moveRel(-3, 29, duration=0.099) 
    pyautogui.moveRel(-1, 31, duration=0.099) 
    pyautogui.moveRel(13, 31, duration=0.099) 
    pyautogui.moveRel(8, 28, duration=0.099) 
    pyautogui.moveRel(13, 21, duration=0.099) 
    pyautogui.moveRel(-17, 12, duration=0.099) 
    pyautogui.moveRel(-42, -3, duration=0.099) 
    pyautogui.moveRel(-21, 2, duration=0.099) 
    pyautogui.moveRel(-15, 7, duration=0.099) 
    pyautogui.moveRel(12, 11, duration=0.099) 
    pyautogui.moveRel(-26, -8, duration=0.099) 
    pyautogui.moveRel(-3, 4, duration=0.099) 
    pyautogui.moveRel(40, 1, duration=0.099) 
    pyautogui.moveRel(19, 7, duration=0.099) 
    pyautogui.moveRel(14, 10, duration=0.099) 
    pyautogui.moveRel(27, 0, duration=0.099) 
    pyautogui.moveRel(33, -10, duration=0.099) 
    pyautogui.moveRel(-21, -2, duration=0.099) 
    pyautogui.moveRel(7, 3, duration=0.099) 
    pyautogui.moveRel(-7, 9, duration=0.099) 
    pyautogui.moveRel(-8, 4, duration=0.099) 
    pyautogui.moveRel(19, -3, duration=0.099) 
    pyautogui.moveRel(5, 6, duration=0.099) 
    pyautogui.moveRel(-20, -1, duration=0.099) 
    pyautogui.moveRel(-33, -4, duration=0.099) 
    pyautogui.moveRel(-45, -21, duration=0.099) 
    pyautogui.moveRel(-14, 1, duration=0.099) 
    return True 



hm = pyHook.HookManager() 
hm.SubscribeMouseLeftDown(go) 
hm.HookMouse() 
pythoncom.PumpMessages() 
hm.UnhookMouse() 
+0

是否有必要解釋您的問題的所有代碼? –

回答

0

你的問題是,在去所有的代碼()是連續的。如果它是迭代的,你可以檢查一個事件或布爾在該循環中觸發並中止go()。 bool/event可以在SubscribeMouseLeftUp()上觸發。這不能在你的情況下迭代地完成。

你可能會試着在你的go()方法中檢查一下觸發器的檢查,但它可能會完成這項工作,而且效率很低。第三,根據鉤子和gui在線程中的工作方式,你可以卸載go()到一個線程。在MouseDown上踢出線程並在MouseUp上放棄/終止它。