Python Pyhook在按下6次后停止捕获关键事件

Python Pyhook在按下6次后停止捕获关键事件,python,windows,python-2.7,pyhook,Python,Windows,Python 2.7,Pyhook,我编写了一个脚本,当使用pyHook按下一个键时,可以移动鼠标。问题是,在6次按键事件之后,脚本停止拾取按键,需要从任务管理器结束 我正在Windows7机器上使用Python2.7。我还没有找到其他人能回答类似的问题 该代码用于钩住鼠标,单击鼠标后移动光标,松开鼠标并钩住键盘。在那里,键盘钩子只对6个事件有效。如果鼠标和键盘都钩住,每个钩子只能处理6个事件。有人知道问题是什么以及如何解决吗 import pythoncom, pyHook, win32api import math from

我编写了一个脚本,当使用pyHook按下一个键时,可以移动鼠标。问题是,在6次按键事件之后,脚本停止拾取按键,需要从任务管理器结束

我正在Windows7机器上使用Python2.7。我还没有找到其他人能回答类似的问题

该代码用于钩住鼠标,单击鼠标后移动光标,松开鼠标并钩住键盘。在那里,键盘钩子只对6个事件有效。如果鼠标和键盘都钩住,每个钩子只能处理6个事件。有人知道问题是什么以及如何解决吗

import pythoncom, pyHook, win32api
import math
from time import sleep

# Radius is 250px
radius = 50

# Intervals in the circle
n_intervals = 50

# List of intervals
l_intervals = []
for i in range(0, n_intervals):
        l_intervals.append((i+1) * math.pi * 2 / n_intervals)
# Move the cursor in a circle
def move_circle():
        (x, y) = win32api.GetCursorPos()
        old_pos = (x, y)
        center = (x-radius, y)
        for i in l_intervals:
                p = (radius * math.cos(i), radius * math.sin(i))
                new_pos = (int(center[0]+p[0]), int(center[1]-p[1]))
                win32api.SetCursorPos(new_pos)
                sleep(0.01)


def OnKeyboardEvent(event):
    if event.Key == "Media_Play_Pause":
        exit()
    else:
        move_circle()

    # return True to pass the event to other handlers
    return True


def OnMouseEvent(event):
    # called when mouse events are received
        if event.MessageName == "mouse left down":
                move_circle() # move the cursor
            hm.UnhookMouse() # unhook the mouse
            hm.HookKeyboard() # hook the keyboard
        return True


hm = pyHook.HookManager()
hm.MouseAll = OnMouseEvent
hm.KeyDown  = OnKeyboardEvent
# Hook the mouse
hm.HookMouse()
# Wait for any events
pythoncom.PumpMessages()

更新:我找到了一个解决方案,并在下面发布了答案,但如果能找到任何能解释我为什么首先遇到这个问题以及为什么解决方案能解决这个问题的答案,我将不胜感激。

在更多的谷歌搜索之后,我找到了一个适合我的解决方案:。我尝试了他的第一个建议,我的问题似乎解决了。我的代码的pyHook部分如下所示

import pythoncom, pyHook, win32api, sys
import math
import threading, time
from time import sleep
...
#attempt to stop pyHook hang...             
lock = threading.Lock()
def KeyEventThread1(i):
    lock.acquire()
    sys.exit()
    lock.release()
def KeyEventThread2(i):
    lock.acquire()
    move_circle()
    lock.release()



def OnKeyboardEvent(event):
    if event.Key == "Media_Play_Pause":
        t = threading.Thread(target=KeyEventThread1, args=(1,))
        t.start()
        sys.exit()
    else:
        t = threading.Thread(target=KeyEventThread2, args=(1,))
        t.start()
    # return True to pass the event to other handlers
    return True

def MouseEventThread(i):
    lock.acquire()
    sleep(.2) #So that mouse is not depressed when moved
    move_circle() # move the cursor
    hm.UnhookMouse() # unhook the mouse
    lock.release()  

def OnMouseEvent(event):
    # called when mouse events are received
    if event.MessageName == "mouse left down":
        t = threading.Thread(target=MouseEventThread, args=(1,))
        t.start()
    hm.HookKeyboard()
    return True

hm = pyHook.HookManager()
hm.MouseAll = OnMouseEvent
hm.KeyDown  = OnKeyboardEvent
# Hook the mouse
hm.HookMouse()
 # hook the keyboard

# Wait for any events
pythoncom.PumpMessages()

经过更多的谷歌搜索,我找到了一个适合我的解决方案:。我尝试了他的第一个建议,我的问题似乎解决了。我的代码的pyHook部分如下所示

import pythoncom, pyHook, win32api, sys
import math
import threading, time
from time import sleep
...
#attempt to stop pyHook hang...             
lock = threading.Lock()
def KeyEventThread1(i):
    lock.acquire()
    sys.exit()
    lock.release()
def KeyEventThread2(i):
    lock.acquire()
    move_circle()
    lock.release()



def OnKeyboardEvent(event):
    if event.Key == "Media_Play_Pause":
        t = threading.Thread(target=KeyEventThread1, args=(1,))
        t.start()
        sys.exit()
    else:
        t = threading.Thread(target=KeyEventThread2, args=(1,))
        t.start()
    # return True to pass the event to other handlers
    return True

def MouseEventThread(i):
    lock.acquire()
    sleep(.2) #So that mouse is not depressed when moved
    move_circle() # move the cursor
    hm.UnhookMouse() # unhook the mouse
    lock.release()  

def OnMouseEvent(event):
    # called when mouse events are received
    if event.MessageName == "mouse left down":
        t = threading.Thread(target=MouseEventThread, args=(1,))
        t.start()
    hm.HookKeyboard()
    return True

hm = pyHook.HookManager()
hm.MouseAll = OnMouseEvent
hm.KeyDown  = OnKeyboardEvent
# Hook the mouse
hm.HookMouse()
 # hook the keyboard

# Wait for any events
pythoncom.PumpMessages()