简体   繁体   English

wxPython:全局捕获关键事件

[英]wxPython: Catching key events globally

There's something I'm trying to do with wxPython and I can't figure out how. 我正试图用wxPython做些什么,我无法弄清楚如何做。 (I'm on Windows 7 and I'm okay with a Windows-only solution.) (我在Windows 7上,我只使用Windows解决方案。)

I want to catch key events globally. 我想在全球范围内捕捉关键事件。 This means key-up, key-down and char events. 这意味着关键,关键和char事件。 (I'm trying to build something like AHK in Python.) (我正在尝试在Python中构建类似AHK的东西。)

Now, I know wxPython allows global hotkeys, but that's not satisfactory, because I want to get all the events, including key up, key down and char. 现在,我知道wxPython允许全局热键,但这并不令人满意,因为我想获得所有事件,包括key up,key down和char。 How can I do that? 我怎样才能做到这一点?

I tried using pyHook, which almost worked except char events aren't implemented. 我尝试使用pyHook,除了char事件之外几乎没有实现。 Char events seem to be tricky and I want to know how to capture them globally. Char事件似乎很棘手,我想知道如何在全球捕获它们。 (ie in all apps.) I'm also okay with solutions that use other tools except wxPython. (即在所有应用程序中。)我也可以使用除wxPython之外的其他工具的解决方案。 (Except not a separate GUI framework, I'm happy with using wxPython for the GUI, just tools for capturing the char events.) (除了没有单独的GUI框架,我很高兴使用wxPython作为GUI,只是用于捕获char事件的工具。)

Sorry, but you can't catch WM_CHAR events directly from a Python executable. 抱歉,您无法直接从Python可执行文件中捕获WM_CHAR事件。 You will need to write a native Windows DLL to hook WH_GETMESSAGE and then separately notify your Python process that a key was pressed. 您需要编写一个本机Windows DLL来挂钩WH_GETMESSAGE,然后单独通知您的Python进程已按下某个键。

As you can see here the way to catch WM_CHAR is to catch the events read by GetMessage() using the WH_GETMESSAGE hook. 正如您在这里看到的,捕获WM_CHAR的方法是使用WH_GETMESSAGE钩子捕获GetMessage()读取的事件。 Sadly, any global hook for that message must be able to run in the context of any process and so has to be implemented as a DLL (as mandated by the API docs ). 遗憾的是,该消息的任何全局钩子必须能够在任何进程的上下文中运行,因此必须作为DLL实现(由API文档强制要求)。 That means you cannot do this inside your Python process. 这意味着您无法在Python进程中执行此操作。 This is also covered in the Python win32 archives here . 这个问题也包括在Python的win32档案这里

That means you need to write a native DLL to hook the messages and then use your favourite form of IPC (eg Post a message to another window) to pass any interesting events to your Python process. 这意味着您需要编写本机DLL来挂钩消息,然后使用您喜欢的IPC形式(例如,将消息发布到另一个窗口)将任何有趣的事件传递给您的Python进程。

If you really just want Python bindings for AutoHotKey, you could use pyahk to do this for you. 如果您真的只想要AutoHotKey的Python绑定,可以使用pyahk为您执行此操作。

The free and open source screen reader for Windows, NVDA, implements this functionality. 适用于Windows的免费开源屏幕阅读器NVDA实现了此功能。 Perhaps take a look at how they accomplish it? 也许看看他们是如何实现的?

Source: winInputHook.py 来源: winInputHook.py

PyWin32 has SetWindowsHook which might work according to this thread . PyWin32有SetWindowsHook ,可能根据这个线程工作 You might be able to use ctypes, although I haven't found any good examples as of yet. 你也许可以使用ctypes,虽然我还没有找到任何好的例子。

I also found this project which looks promising: 我还发现这个看起来很有希望的项目:

This may not be an ideal solution, but have tried including some pygame code? 这可能不是一个理想的解决方案,但尝试包含一些pygame代码?

Pygame is a module used to build games (as the name suggests), but it has some nice functions for getting keypresses. Pygame是一个用于构建游戏的模块(顾名思义),但它有一些很好的功能来获取按键。

This tutorial shows how to get keyboard input, it also has pygame do some grpahical stuff, but you don't need to have any of that to get key presses. 本教程展示了如何获得键盘输入,它还有pygame做一些grpahical的东西,但你不需要任何这些来获得按键输入。

http://www.nerdparadise.com/tech/python/pygame/basics/part6/ http://www.nerdparadise.com/tech/python/pygame/basics/part6/

And here is a list of the key codes pygame uses. 这里是pygame使用的关键代码列表。

https://www.pygame.org/docs/ref/key.html https://www.pygame.org/docs/ref/key.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM