本文介绍了延迟鼠标点击 0.5 秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此代码在 Windows 中使用 pyhook 检测鼠标位置.我需要的是检测鼠标点击并在执行前添加延迟 - 场景:我用鼠标点击但这次点击应该延迟 0.5 秒(所以点击应该在 0.5 秒后执行).这有可能吗?
导入pyHook导入pythoncom定义点击(事件):打印 event.Position返回真hm = pyHook.HookManager()hm.SubscribeMouseAllButtonsDown(onclick)hm.HookMouse()pythoncom.PumpMessages()hm.UnhookMouse()
解决方案
像这样:
导入pyHook导入pythoncom导入时间导入线程_HK 类:def __init__(self):self.ev = 无def run(self,passarg):pythoncom.CoInitialize()而真:如果 self.ev != None :时间.睡眠(1)打印 self.ev.Positionself.ev = 无HK = _HK()s = thread.start_new_thread(HK.run,(None,))定义点击(事件):HK.ev = 事件返回真hm = pyHook.HookManager()hm.SubscribeMouseAllButtonsDown(onclick)hm.HookMouse()pythoncom.PumpMessages()hm.UnhookMouse()
如果应用程序专注于一个过程.最初,该过程必须由系统解决.它可以然后访问内部和外部组件.pythoncom.CoInitialize()
需要一个函数或附加进程!
我希望它有帮助(已测试).
I am using this code to detect mouse position with pyhook in Windows. What I need is to detect mouse click and add delay before execution - scenario: I click with mouse but this click should be 0.5 second delayed (so click should be executed after 0.5 second). Is this possible somehow?
import pyHook
import pythoncom
def onclick(event):
print event.Position
return True
hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(onclick)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()
解决方案
Like this :
import pyHook
import pythoncom
import time
import thread
class _HK :
def __init__(self):
self.ev = None
def run(self,passarg):
pythoncom.CoInitialize()
while True :
if self.ev != None :
time.sleep(1)
print self.ev.Position
self.ev = None
HK = _HK()
s = thread.start_new_thread(HK.run,(None,))
def onclick(event):
HK.ev = event
return True
hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(onclick)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()
I hope it helps(TESTED).
这篇关于延迟鼠标点击 0.5 秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!