如何仅在按住鼠标单击时激活循环? (释放按键时停止循环)PYTHON 3
How to activate the loop only if mouse click is being held down? (Stop the loop when releasing the key) PYTHON 3
我这里有一个相当简单的脚本:
import win32api
def press():
lmb_status = win32api.GetKeyState(0x01)
return lmb_status < 0
while True:
if lmb_status():
print ("print this")
win32api.Sleep(1000)
print ("do not print this")
问题是如果我单击鼠标左键,脚本会作为一个整体执行,但我的主要目标是脚本应该在松开按键后立即停止。因此,如果我点击(如果它在一秒钟内释放)它应该只激活第一个命令 "print this",但它不应该打印第二个。但它确实如此,它没有像我希望的那样工作。
我怎样才能让它发挥作用?
感谢您的帮助。
编辑 1:
阅读了这两条有用的评论后,我认为我的方向是正确的,但它仍然无法正常工作:当我释放点击时,"Interruption." 只有在两者都出现时才会执行打印命令已经完成。
import win32api
def click():
left = win32api.GetKeyState(0x01)
return left < 0
def noclick():
left = win32api.GetKeyState(0x01)
return left >= 0
while True:
function = False
win32api.Sleep(10)
if click():
function = True
elif noclick():
function = False
if function == False:
print("Interruption.")
if function == True:
print ("This should be printed.")
win32api.Sleep(1000)
print ("This should not.")
您可以使用 pyHook
获取鼠标点击事件,并创建一个线程 运行 当按下左键时循环,并在按钮弹起时终止线程。
import win32api, win32gui, pyHook, pythoncom, ctypes, inspect, sys, _thread
kernel32 = ctypes.windll.kernel32
ThreadId = 0
def loop():
while True:
print ("This should be printed.")
win32api.Sleep(1000)
print ("This should not.")
def onMouse_leftdown(event):
global ThreadId
if(ThreadId == 0):
print("start loop")
ThreadId = _thread.start_new_thread(loop,())
return True
return True
def onMouse_leftup(event):
global ThreadId
if(ThreadId != 0):
print("end loop")
hThread = kernel32.OpenThread(0x0001, 0, ThreadId)
ret = kernel32.TerminateThread(hThread,0)
ThreadId = 0
return True
return True
if __name__ == "__main__":
hm = pyHook.HookManager()
hm.MouseLeftDown = onMouse_leftdown
hm.MouseLeftUp = onMouse_leftup
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()
我这里有一个相当简单的脚本:
import win32api
def press():
lmb_status = win32api.GetKeyState(0x01)
return lmb_status < 0
while True:
if lmb_status():
print ("print this")
win32api.Sleep(1000)
print ("do not print this")
问题是如果我单击鼠标左键,脚本会作为一个整体执行,但我的主要目标是脚本应该在松开按键后立即停止。因此,如果我点击(如果它在一秒钟内释放)它应该只激活第一个命令 "print this",但它不应该打印第二个。但它确实如此,它没有像我希望的那样工作。
我怎样才能让它发挥作用?
感谢您的帮助。
编辑 1:
阅读了这两条有用的评论后,我认为我的方向是正确的,但它仍然无法正常工作:当我释放点击时,"Interruption." 只有在两者都出现时才会执行打印命令已经完成。
import win32api
def click():
left = win32api.GetKeyState(0x01)
return left < 0
def noclick():
left = win32api.GetKeyState(0x01)
return left >= 0
while True:
function = False
win32api.Sleep(10)
if click():
function = True
elif noclick():
function = False
if function == False:
print("Interruption.")
if function == True:
print ("This should be printed.")
win32api.Sleep(1000)
print ("This should not.")
您可以使用 pyHook
获取鼠标点击事件,并创建一个线程 运行 当按下左键时循环,并在按钮弹起时终止线程。
import win32api, win32gui, pyHook, pythoncom, ctypes, inspect, sys, _thread
kernel32 = ctypes.windll.kernel32
ThreadId = 0
def loop():
while True:
print ("This should be printed.")
win32api.Sleep(1000)
print ("This should not.")
def onMouse_leftdown(event):
global ThreadId
if(ThreadId == 0):
print("start loop")
ThreadId = _thread.start_new_thread(loop,())
return True
return True
def onMouse_leftup(event):
global ThreadId
if(ThreadId != 0):
print("end loop")
hThread = kernel32.OpenThread(0x0001, 0, ThreadId)
ret = kernel32.TerminateThread(hThread,0)
ThreadId = 0
return True
return True
if __name__ == "__main__":
hm = pyHook.HookManager()
hm.MouseLeftDown = onMouse_leftdown
hm.MouseLeftUp = onMouse_leftup
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()