After more google-searching I found a solution that worked for me: pyHook + pythoncom stop working after too much keys pressed [Python]. I tried his first suggestion, and my problem appears solved. The pyHook part of my code now looks as below.
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()