0

我用 Python 写了一个小程序,当我按下鼠标左键时,它会执行函数“go”。然而,我的意图是仅在我持有 LMB 时执行 go。如果我释放 LMB,它应该取消“go”。最后,当取消或成功执行时,当我再次按下 LMB 时,它应该准备好做同样的事情。

这是代码:

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()
4

1 回答 1

0

Your issue is that all your code in go() is sequential. If it were iterative, you could've checked for an event or bool to be triggered in that loop and abort go(). The bool/event could be triggered on SubscribeMouseLeftUp(). This cannot be done iteratively in your case.

You could perhaps try and pepper a check for trigger in your go() method its not clean but it could do the job, be it in a very inefficient manner.

Thirdly, depending on how hooks and gui work in threads you could offload go() to a thread. Kick that thread off on MouseDown and abort/kill it on MouseUp.

于 2016-03-08T09:09:16.760 回答