我正在尝试使用pythoncom和pyhook来禁用用户鼠标,同时我的程序对其进行了某些处理。 然后将控制权交还给用户。
运行此命令时:
import pythoncom, pyHook, time, threading, win32api, win32con
def click(x, y):
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
time.sleep(0.2)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
class record(object):
def OnMouseEvent(self, event):
print 'MessageName:',event.MessageName
print 'Message:',event.Message
print 'Time:',event.Time
print 'Window:',event.Window
print 'WindowName:',event.WindowName
print 'Position:',event.Position
print 'Wheel:',event.Wheel
print 'Injected:',event.Injected
print '---'
# returning false makes windows ignore the event
# to block the user mouse just return false for injected = 0 and true for injected = 1
return True
class MouseEventPumper(threading.Thread):
def run(self):
recordit = record()
hm = pyHook.HookManager()
hm.MouseAll = recordit.OnMouseEvent
hm.HookMouse()
self.pumpIt = True
while self.pumpIt:
pythoncom.PumpWaitingMessages()
print "thread terminated"
def stop(self):
self.pumpIt = False
if __name__ == '__main__':
thread = MouseEventPumper(name = "Mouse event pumper")
thread.start()
# in my real program here I will do a lot of things, the purpose is to block the user
# mouse while I use win32api to control the mouse then once I'm done I'm giving back control to the user
click(50, 50)
time.sleep(3)
print "Stopping mouse event pumper"
thread.stop()
print "stopped"
最后我得到
runtime error R6031
- Attempt to initialize the CRT more than once.
This indicates a bug in your application.
我不确定,但我认为这是图书馆的一个问题。 你觉得呢?
谢谢。
Windows 10上的Python 2.7 x64