首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pyHook KeyLogger线程未完成

pyHook KeyLogger线程未完成
EN

Stack Overflow用户
提问于 2014-05-07 19:08:24
回答 2查看 803关注 0票数 2

我为一个键盘记录器创建了一个线程,它与另一个产生声音的线程并行记录(我想捕捉反应时间)。

不幸的是,尽管我调用了killKey()并且输出了“调用的killkey()”,但线程始终没有结束。

我总是从这个线程得到一个thread.isActive() = true。

代码语言:javascript
复制
class KeyHandler(threading.Thread):
    hm = pyHook.HookManager()

    def __init__(self): 
       threading.Thread.__init__(self) 

    def OnKeyboardCharEvent(self,event):
        print 'Key:', event.Key
        if event.Key=='E':
            ...   
        return True

    def killKey(self):
        KeyHandler.hm.UnhookKeyboard() 
        ctypes.windll.user32.PostQuitMessage(0) 
        print "invoked killkey()"

    def run(self):
        print "keyHandlerstartetrunning"
        KeyHandler.hm.KeyDown = self.OnKeyboardCharEvent
        KeyHandler.hm.HookKeyboard()
        #print "keyboardhooked"
        pythoncom.PumpMessages()

更准确地说,ctypes.winll.user32.PostQuitMessage(0)什么也不做

我倾向于使用外部超时来调用killKey(),在这个线程中调用相应的ctyes.winll.user32.PostQuitMessage(0)。

EN

回答 2

Stack Overflow用户

发布于 2014-05-07 20:44:53

PostQuitMessage必须从相同的线程发布。为此,您需要引入一个全局变量STOP_KEY_HANDLER。如果你想退出,那么只需从你想要的任何线程设置全局STOP_KEY_HANDLER = True,它将在下一次击键时退出。您的按键处理程序必须在主线程上运行。

代码语言:javascript
复制
STOP_KEY_HANDLER = False

def main():
    pass # here do all you want
    #bla bla
    global STOP_KEY_HANDLER
    STOP_KEY_HANDLER = True # This will kill KeyHandler


class KeyHandler:
    hm = pyHook.HookManager()

    def OnKeyboardCharEvent(self,event):
        if STOP_KEY_HANDLER:
            self.killKey()
        print 'Key:', event.Key
        if event.Key=='E':
            pass
        return True

    def killKey(self):
        global STOP_KEY_HANDLER
        if not STOP_KEY_HANDLER:
            STOP_KEY_HANDLER = True
            return None
        KeyHandler.hm.UnhookKeyboard()
        ctypes.windll.user32.PostQuitMessage(0)
        print "invoked killkey()"

    def _timeout(self):
        if self.timeout:
            time.sleep(self.timeout)
            self.killKey()

    def run(self, timeout=False):
        print "keyHandlerstartetrunning"
        self.timeout = timeout
        threading.Thread(target=self._timeout).start()

        KeyHandler.hm.KeyDown = self.OnKeyboardCharEvent
        KeyHandler.hm.HookKeyboard()
        #print "keyboardhooked"
        pythoncom.PumpMessages()


k=KeyHandler()

threading.Thread(target=main).start()
k.run(timeout=100) # You can specify the timeout in seconds or you can kill it directly by setting STOP_KEY_HANDLER to True.
票数 2
EN

Stack Overflow用户

发布于 2014-05-07 22:50:07

我想pbackup的解决方案很好。总而言之,我找到了一个解决方案,只需自己发送一个键,而不是等待用户输入。这显然不是最好的,但在我的计时线程中,它是与其他计时例程并行运行的最快的。

代码语言:javascript
复制
    STOP_KEY_HANDLER = True

    # send key to kill handler - not pretty but works
    for hwnd in get_hwnds_for_pid (GUIWINDOW_to_send_key_to.pid):
        win32gui.PostMessage (hwnd, win32con.WM_KEYDOWN, win32con.VK_F5, 0)
    # sleep to make sure processing is done
    time.sleep(0.1)

    # kill window
    finished()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23516150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档