python 是否有跨平台的python 低级API 来捕获或生成键盘事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/676713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:36:45  来源:igfitidea点击:

Is there a cross-platform python low-level API to capture or generate keyboard events?

pythoncross-platformkeyboard-eventslow-level-api

提问by MiniQuark

I am trying to write a cross-platform python program that would run in the background, monitor all keyboard events and when it sees some specific shortcuts, it generates one or more keyboard events of its own. For example, this could be handy to have Ctrl-@ mapped to "my.email@address", so that every time some program asks me for my email address I just need to type Ctrl-@.

我正在尝试编写一个跨平台的 python 程序,它会在后台运行,监视所有键盘事件,当它看到一些特定的快捷键时,它会生成一个或多个自己的键盘事件。例如,将 Ctrl-@ 映射到“my.email@address”可能很方便,这样每次某个程序询问我的电子邮件地址时,我只需要键入 Ctrl-@。

I know such programs already exist, and I am reinventing the wheel... but my goal is just to learn more about low-level keyboard APIs. Moreover, the answer to this question might be useful to other programmers, for example if they want to startup an SSH connection which requires a password, without using pexpect.

我知道这样的程序已经存在,我正在重新发明轮子……但我的目标只是了解更多关于低级键盘 API 的信息。此外,这个问题的答案可能对其他程序员有用,例如,如果他们想启动需要密码的 SSH 连接,而不使用 pexpect。

Thanks for your help.

谢谢你的帮助。

Note: there is a similar questionbut it is limited to the Windows platform, and does not require python. I am looking for a cross-platform python api. There are also other questions related to keyboard events, but apparently they are not interested in system-wide keyboard events, just application-specific keyboard shortcuts.

注意:有一个类似的问题,但仅限于Windows平台,不需要python。我正在寻找一个跨平台的python api。还有其他与键盘事件相关的问题,但显然他们对系统范围的键盘事件不感兴趣,只对特定于应用程序的键盘快捷键感兴趣。

Edit: I should probably add a disclaimer here: I do notwant to write a keylogger. If I needed a keylogger, I could download one off the web a anyway. ;-)

编辑:我应该在这里增加一个声明:我不是想写一个键盘记录。如果我需要一个键盘记录器,我可以从网上下载一个。;-)

采纳答案by Aaron Digulla

There is no such API. My solution was to write a helper module which would use a different helper depending on the value of os.name.

没有这样的 API。我的解决方案是编写一个助手模块,该模块将根据os.name.

On Windows, use the Win32 extensions.

在 Windows 上,使用Win32 扩展

On Linux, things are a bit more complex since real OSes protect their users against keyloggers[*]. So here, you will need a root process which watches one of[] the handles in /dev/input/. Your best bet is probably looking for an entry below /dev/input/by-path/which contains the strings "kbd"or "keyboard". That should work in most cases.

在 Linux 上,事情有点复杂,因为真正的操作系统保护他们的用户免受键盘记录器的侵害[*]。因此,在这里,您将需要一个根进程来监视/dev/input/. 您最好的选择可能是在下面寻找/dev/input/by-path/包含字符串"kbd""keyboard". 这应该在大多数情况下有效。

[*]: Jeez, not even my virus/trojan scanner will complain when I start a Python program which hooks into the keyboard events...

[*]:天哪,当我启动一个与键盘事件挂钩的 Python 程序时,即使我的病毒/木马扫描程序也不会抱怨......

回答by Aaron Digulla

As the guy that wrote the original pykeylogger linux port, I can say there isn't really a cross platform one. Essentially I rewrote the pyhook API for keyboard events to capture from the xserver itself, using the record extension. Of course, this assumes the record extension is there, loaded into the x server.

作为编写原始 pykeylogger linux 端口的人,我可以说没有真正的跨平台。本质上,我使用记录扩展重写了键盘事件的 pyhook API,以便从 xserver 本身捕获。当然,这假设记录扩展在那里,加载到 x 服务器中。

From there, it's essentially just detecting if you're on windows, or linux, and then loading the correct module for the OS. Everything else should be identical.

从那里开始,它本质上只是检测您是在 Windows 上还是在 linux 上,然后为操作系统加载正确的模块。其他一切都应该是相同的。

Take a look at the pykeylogger source, in pyxhook.py for the class and implimentation. Otherwise, just load that module, or pyhook instead, depending on OS.

查看 pykeylogger 源代码,在 pyxhook.py 中了解类和实现。否则,只需加载该模块或 pyhook,具体取决于操作系统。

回答by PeiMei

I've made a few tests on Ubuntu 9.10. pykeylogger doesn't seems to be working. I've tryied to change the /etc/X11/xorg.conf in order to allow module to be loaded but in that specific version of ubuntu there is no xorg.conf. So, in my opiniion pykelogger is NOT working on ubuntu 9.10 !!

我在 Ubuntu 9.10 上做了一些测试。pykeylogger 似乎不起作用。我已经尝试更改 /etc/X11/xorg.conf 以允许加载模块,但在该特定版本的 ubuntu 中没有 xorg.conf。所以,在我看来,pykelogger 不适用于 ubuntu 9.10 !!

回答by Chris McCormick

Under Linux it's possible to do this quite easily with Xlib. See this page for details:

在 Linux 下,使用 Xlib 可以很容易地做到这一点。有关详细信息,请参阅此页面:

http://www.larsen-b.com/Article/184.html

http://www.larsen-b.com/Article/184.html

回答by vartec

Cross-platform UI libraries such as Tkinteror wxPythonhave API for keyboard events. Using these you could map ?CTRL? + ?@? to an action.

TkinterwxPython等跨平台 UI 库具有用于键盘事件的 API。使用这些你可以映射 ?CTRL? + ?@? 到一个动作。

回答by Kim Stebel

On linux, you might want to have a look at pykeylogger. For some strange reason, reading from /dev/input/.... doesn't always work when X is running. For example it doesn't work on ubuntu 8.10. Pykeylogger uses xlib, which works exactly when the other way doesn't. I'm still looking into this, so if you find a simpler way of doing this, please tell me.

在 linux 上,您可能想看看pykeylogger。出于某种奇怪的原因,当 X 运行时,从 /dev/input/.... 读取并不总是有效。例如,它不适用于 ubuntu 8.10。Pykeylogger 使用 xlib,当另一种方式不起作用时,它可以正常工作。我还在研究这个,所以如果你找到一个更简单的方法,请告诉我。