使用 python (后台守护进程)检查是否按下了键
我创建了一个 python 脚本,其中每次按下键盘上的 Super(或 WinKey)时都需要执行一个事件。
如何在 python 进程不被“聚焦”的情况下实现这一目标——因为它在后台运行,等待按下按键来执行事件?
我在网上看到了很多帖子,向我展示了如何读取输入 - 但它们都需要一个过程“集中”,并且没有一个帖子向我展示了如何使用 python 脚本捕获 Super(或 WinKey)。
我运行的是 Ubuntu 9.10。
I've created a python script in which an event needs to be executed each time I press the Super (or WinKey) on my keyboard.
How can one achieve this without the python process being "focused" - as it is running in the background waiting for the key to be pressed to execute the event?
I've seen a lot of posts around the web showing me how to read input - but they have all required one to have the process "focused" and none have showed me how to capture the Super (or WinKey) using a python script.
I'm running Ubuntu 9.10.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这使我能够获取 *nix 系统上修饰符键的状态。
要从另一个文件将状态调用到字典,只需调用
from FILENAME import *
并执行以下操作:为可以找到代码的创建者干杯 此处,以及
This allows me to get the state of modifier keys on my *nix system.
To call state to a dictionary,from another file, just call
from FILENAME import *
and do the following:Cheers to the creator whose code can be found here, and to Jason Orendorffl for drawing my attention to it.
我之前的回答显然是完全错误的,抱歉。
我认为正确的方法是从 /dev/input/event1 读取(?)
这个简短的测试为我显示了扫描码,即使终端没有焦点:
我不知道 /dev/input/event1 是否始终是键盘或如何确定哪个是键盘,但至少对我来说它有效
My previous answer apparently was completely wrong, sorry.
I think the correct approach would be to read from /dev/input/event1 (?)
This short test showed scancodes for me, even if the terminal did not have focus:
I do not know if /dev/input/event1 is always the keyboard or how to determine which one is, but at least for me it worked
您可以使用 PyUserInput 来实现此目的,这是一个跨平台解决方案,在您的情况下兼容,与X11。
超级示例:
You can use PyUserInput for this purpose, which is a cross-platform solution, compatible, in your case, with X11.
Example with Super:
如果您需要在 Linux 上工作,我认为您需要以某种方式监视 X 事件。
如果运行命令“xev”,您可以看到所有事件 - 所以我确信 X 提供了所有事件的接口。快速谷歌搜索找到 Python X 库,它可能会为您提供您需要的东西。
您可以 在这里下载。
If you need to work on Linux, I think you need to somehow monitor for X events.
You can see all events if you run the command "xev" - so I am sure that X provides an interface to all events. A quick googling finds Python X library that probably gives you the stuff you need.
You can download it here.