Python 检测发送到另一个应用程序的击键

发布于 2024-08-18 14:57:44 字数 259 浏览 10 评论 0原文

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

I have a Python program that sends keystrokes to another application using SendKeys. Some of the keystrokes, however, must be sent to the application after it does some processing (which takes an unknown amount of time). So far I have had to let the Python application know the processing was finished by Alt+Tabbing back to the DOS window and hitting Enter. I'd like to have a key combination (Shift+F1 or something like that) that I can hit in the receiving application that signals the Python program to continue without me having to switch back to the DOS window. How would I make it so I can detect keystrokes in Python even though the focus is on another window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2024-08-25 14:57:44

看看 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()

Have a look at pyHook.

It allows Keyboard hooking:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文