1
我寫了一個腳本來移動鼠標,當使用pyHook按下某個鍵時。問題是,在6次按鍵事件之後,腳本停止拾取按鍵,需要從任務管理器中結束。Pyhook在6次按下後停止捕獲關鍵事件
我在Windows 7機器上使用python 2.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()
更新:我找到了解決方案,並張貼下面的答案,但仍然會明白,可以解釋爲什麼我在首位有問題的任何答案,爲什麼解決方案修復它。