按下6键后Pyhook停止捕获关键事件 [英] Pyhook stops capturing key events after 6 presses

查看:341
本文介绍了按下6键后Pyhook停止捕获关键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a script to move the mouse around when a key is pressed using pyHook. The problem is that after 6 key press events the script stops picking up key presses and needs to be ended from task manager.

我在Windows 7计算机上使用python 2.7.我没有找到其他人可以解决类似问题.

I am using python 2.7 on a Windows 7 machine. I have not found anyone else with an answer to a similar problem.

该代码旨在钩住鼠标,然后单击它即可移动光标,解开鼠标并钩住键盘.在那里,键盘挂钩仅可用于6个事件.如果我同时钩住鼠标和键盘,则每个钩子仅可用于6个事件.有谁知道这个问题是什么以及如何解决?

The code is designed to hook the mouse, and then once it is clicked move the cursor, unhook the mouse and hook the keyboard. There, the keyboard hook only works for 6 events. If I keep both the mouse and keyboard hooked, each hook works for only 6 events. Does anyone have any ideas what the problem is and how to fix it?

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()

更新:我找到了一个解决方案,并将答案发布在下面,但仍然可以通过任何答案来解释为什么我首先遇到问题以及解决方案可以解决该问题.

UPDATE: I found a solution and posted the answer below, but would still appreciate any answers that can explain why I had the problem in the first place, and why the solution fixes it.

推荐答案

经过更多的google搜索,我发现了一个对我有用的解决方案:

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()

这篇关于按下6键后Pyhook停止捕获关键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆