Python在KeyDown [ctrl] + [c]上使用pyHook.HookManager()崩溃 [英] Python crash with pyHook.HookManager() on KeyDown [ctrl]+[c]

查看:221
本文介绍了Python在KeyDown [ctrl] + [c]上使用pyHook.HookManager()崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个python脚本来记录我在系统上按下的键(键盘记录器),并通过输入有关自己的打字习惯的信息回电.我是python的新手,我正在使用此应用程序来了解它.我正在使用python-3.x和Windows 8

I am creating a python script to record the keys that I press across my system (keylogger) and call home with information on what my typing habits are. I am new to python and I am using this application to learn about it. I am using python-3.x and windows 8

完整的源代码可以在这里找到 https://github.com/funvill/QSMonitor/blob/master/monitor.py

Full source code can be found here https://github.com/funvill/QSMonitor/blob/master/monitor.py

使用此代码段,我可以记录整个系统中的所有按键.问题是当我[ctrl] + [c]在另一个窗口中复制某些内容时,python代码崩溃了.

Using this snippet I am able to record all the keypress across my entire system. The problem is when I [ctrl]+[c] to copy something in another window the python code crashes.

复制步骤

  1. 运行monitor.py脚本
  2. 在另一个窗口(notepad.exe)中,输入几个字母(abcd).
  3. 突出显示记事本中的字母并按住[ctrl]并按[c]

经验丰富的:

将弹出Windows错误消息,并告诉我python.exe已停止运行,需要重新启动.命令窗口中没有python错误消息.

An windows error message would pop up and tell me that the python.exe has stop functioning and would need to be restarted. No python error message was in the command window.

def OnKeyboardEvent(event):
    global keyDatabase
    global keyLoggerCount 
    keyLoggerCount += 1 

    # http://code.activestate.com/recipes/553270-using-pyhook-to-block-windows-keys/ 
    print ('MessageName:',event.MessageName )
    print ('Message:',event.Message)
    print ('Time:',event.Time)
    print ('Window:',event.Window)
    print ('WindowName:',event.WindowName)
    print ('Ascii:', event.Ascii, chr(event.Ascii) )
    print ('Key:', event.Key)
    print ('KeyID:', event.KeyID)
    print ('ScanCode:', event.ScanCode)
    print ('Extended:', event.Extended)
    print ('Injected:', event.Injected)
    print ('Alt', event.Alt)
    print ('Transition', event.Transition)
    print ('---')    

    # check to see if this key has ever been pressed before 
    # if it has not then add it and set its start value to zero. 
    if event.Key not in keyDatabase:
        keyDatabase[ event.Key ] = 0 ; 

    # Incurment the key value 
    keyDatabase[ event.Key ] += 1 ; 
    return True

# When the user presses a key down anywhere on their system 
# the hook manager will call OnKeyboardEvent function.     
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()

while True : 
    pythoncom.PumpWaitingMessages()

我的问题是:

  • 是什么原因导致复制和过去的崩溃?

推荐答案

如果要捕获KeyboardInterrupt异常,则可以使用嵌套循环.这样,当发生KeyboardInterrupt时,程序仅退出内部循环.

If you want to catch KeyboardInterrupt exceptions, you could use a nested loop. This way when a KeyboardInterrupt occurs, the program exits only the inner loop.

while True:
    try:
        while True:
            pythoncom.PumpWaitingMessages()
    except KeyboardInterrupt:
        pass

这篇关于Python在KeyDown [ctrl] + [c]上使用pyHook.HookManager()崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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