【问题标题】:Python Detect Keystrokes Sent to Another ApplicationPython检测发送到另一个应用程序的击键
【发布时间】:2010-01-20 09:40:28
【问题描述】:

我有一个 Python 程序,它使用 SendKeys 将击键发送到另一个应用程序。但是,某些击键必须在应用程序进行一些处理后发送到应用程序(这需要未知的时间)。到目前为止,我不得不让 Python 应用程序知道处理已通过 Alt+Tabbing 返回 DOS 窗口并按 Enter 完成。我想要一个组合键(Shift+F1 或类似的东西),我可以在接收应用程序中点击它,通知 Python 程序继续,而无需切换回 DOS 窗口。即使焦点在另一个窗口上,我如何才能检测到 Python 中的击键?

【问题讨论】:

    标签: python sendkeys


    【解决方案1】:

    看看pyHook

    它允许键盘挂钩:

    import pythoncom, 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 to pass the event to other handlers
      return True
    
    # create a hook manager
    hm = pyHook.HookManager()
    # watch for all mouse events
    hm.KeyDown = OnKeyboardEvent
    # set the hook
    hm.HookKeyboard()
    # wait forever
    pythoncom.PumpMessages()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多