本文介绍了pyHook停止(随机)接收按键事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,可以使用pyHook捕获所有按键,然后运行一些功能.
我注意到一段时间(随机持续时间)后,即使我按了按键,程序仍停止接收按键触发信号?
pyHook不稳定吗?
我不会更改按下的键或过早按下它们之类的东西.
这是我的代码:

I have a program that captures all key presses using pyHook, then runs a few functions.
I notice that after a while (of random duration), the program was stop receiving key triggers, even though I am pressing keys?
Is pyHook unstable?
I'm not changing what keys are pressed or pressing them prematurely or anything like that.
Here's my code:

import time
import win32api
import win32con
import pythoncom
import pyHook
import os
import ctypes

def Click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

def DeleteRun(event):
    if event.Ascii == 96:
        BreakHook()
        return False
    Click(1250, 741)
    time.sleep(0.2)
    Click(649,261)
    time.sleep(0.2)
    Click(651, 348)
    time.sleep(0.2)
    Click(800, 442)
    time.sleep(0.2)
    Click(865, 612)
    time.sleep(0.2)
    Click(25, 744)
    time.sleep(3)
    Click(25, 744)
    time.sleep(1.5)
    Click(1112,297)
    Click(145,392)
    return True

def BreakHook():
    ctypes.windll.user32.PostQuitMessage(0)

KeyGrabber = pyHook.HookManager()
KeyGrabber.KeyDown = DeleteRun
KeyGrabber.HookKeyboard()
pythoncom.PumpMessages()

为什么突然停止工作?
即使我通过IDE停止程序,该过程在我的计算机上仍处于活动状态,这非常令人沮丧.

Why does is suddenly stop working?
It's very frustrating as the process remains active on my computer, even if I stop the program through the IDE.

规格:
python 2.7.2
Windows 7(32)

Specs:
python 2.7.2
Windows 7 (32)

推荐答案

类似的问题(我敢说完全相同吗?),这里讨论并解决了这些问题:并在此处: Pyhook在按下6次后将停止捕获关键事件

Similar (dare I say identical?) problems were discussed and resolved here: pyHook + pythoncom stop working after too much keys pressed [Python]and here: Pyhook stops capturing key events after 6 presses

这篇关于pyHook停止(随机)接收按键事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 02:55