我正在Windows 10上使用pyHook 1.5.1运行python 2.7.15。
所以我有一个pygame窗口来显示内容,我使用pyhook有时禁用用户鼠标并以编程方式移动它。
问题是,如果我将鼠标移到pygame窗口上方,则会变得缓慢并且脚本停止运行(控制台中的打印停止,然后在10-30秒后恢复循环)
这是我的代码来演示该问题,运行它,然后将鼠标移到pygame窗口上,查看控制台,鼠标也会变松。
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import datetime
import time
import pygame
import pythoncom, pyHook, threading
class AllowOnlyInjectedEvents(object):
def OnMouseEvent(self, event):
if event.Injected == 1:
return True
else:
# Not injected, returning False makes windows ignore the event
return False
class AllowAllEvents(object):
def OnMouseEvent(self, event):
return True
class MouseEventPumper(threading.Thread):
def run(self):
self.allowAllEvents = AllowAllEvents()
self.allowOnlyInjected = AllowOnlyInjectedEvents()
self.hm = pyHook.HookManager()
self.hm.MouseAll = self.allowAllEvents.OnMouseEvent
self.hm.HookMouse()
pythoncom.PumpMessages()
print "thread terminated"
def switchToOnlyInjectedEvents(self):
self.hm.MouseAll = self.allowOnlyInjected.OnMouseEvent
print "Switched to only injected mouse event"
def switchToAllEvents(self):
self.hm.MouseAll = self.allowAllEvents.OnMouseEvent
print "switched to all mouse event"
window_resolution = (600, 600)
window = pygame.display.set_mode(window_resolution)
background_color = (50 , 50, 50)
origin_position = (0, 0)
background_surface = pygame.Surface(window_resolution)
background_surface.fill(background_color)
window.blit(background_surface, origin_position)
#init mouse pumper
mouseEventPumper = MouseEventPumper(name = "Mouse event pumper")
mouseEventPumper.start()
while True:
ts = time.time()
date_as_text = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H.%M.%S')
print "{} : loop iteration".format(date_as_text)
pygame.display.flip()
禁用/启用用户鼠标效果很好,我唯一的问题是我可以将鼠标悬停在pygame窗口上。
我尝试使用cv2.imshow()而不是pygame显示内容,并且执行相同的操作。
关于问题可能是什么的任何想法? 谢谢。