【问题标题】:Using pyHook to detect key up and down使用pyHook检测key up和down
【发布时间】:2017-04-14 18:59:19
【问题描述】:

我发现使用 pyHook 可以打印鼠标上下点击的脚本:

class record(object):
    def OnMouseEvent(self, event):
        print 'MessageName:',event.MessageName
        print 'Message:',event.Message
        print 'Time:',event.Time
        print 'Window:',event.Window
        print 'WindowName:',event.WindowName
        print 'Position:',event.Position
        print 'Wheel:',event.Wheel
        print 'Injected:',event.Injected
        print '---'
        #time.sleep(1) #If I uncomment this, running the program will freeze stuff, as mentioned earlier.
        return True

Record = record()
hm = pyHook.HookManager()
hm.MouseAll = Record.OnMouseEvent
hm.HookMouse()
pythoncom.PumpMessages()

当我使用 pyHook 以相同的方式检测键盘上的上下键时,它只显示了按键

def OnKeyboardEvent(event): 
    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 ('---')    
    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()
try:
    pythoncom.PumpMessages()
except KeyboardInterrupt:
    pass

我怎样才能检测到按键?

【问题讨论】:

    标签: python keylogger pyhook


    【解决方案1】:

    这真的很晚了,但希望它可以帮助某人。您只是将钩子管理器注册到按键事件,所以这些是唯一显示的。您还需要订阅 KeyUp 事件。您可以将它们注册到相同的函数,如图所示,但请注意,可能对于一个项目,您可能希望为它们订阅不同的方法。

    def OnKeyboardEvent(event): 
        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 ('---')    
        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
    # Here we register the same function to the KeyUp event. 
    # Probably in practice you will create a different function to handle KeyUp functionality
    hm.KeyUp = OnKeyboardEvent
    hm.HookKeyboard()
    try:
        pythoncom.PumpMessages()
    except KeyboardInterrupt:
        pass
    

    此外,根据您的 Python 版本,如果您在 OnKeyboardEvent 结束时未返回 True,则可能会遇到错误。您可能还想花一些时间阅读 HookManager.py。快乐的键盘记录!呃呃,孩子们注意安全

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多