是否有跨平台的python低级API来捕获或生成键盘事件? [英] Is there a cross-platform python low-level API to capture or generate keyboard events?

查看:106
本文介绍了是否有跨平台的python低级API来捕获或生成键盘事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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-@.

我知道这样的程序已经存在,并且我正在重新发明轮子……但是我的目标只是学习更多有关低级键盘API的知识.此外,此问题的答案可能对其他程序员有用,例如,如果其他程序员想要在不使用pexpect的情况下启动需要密码的SSH连接,则为

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.

感谢您的帮助.

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

Note: there is a similar question but 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.

我可能应该在此处添加免责声明:我要编写键盘记录程序.如果我需要键盘记录器,我仍然可以从网络上下载一个键盘记录器. ;-)

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

推荐答案

没有此类API.我的解决方案是编写一个帮助程序模块,该模块将根据os.name的值使用不同的帮助程序.

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.

在Windows上,使用 Win32扩展.

On Windows, use the Win32 extensions.

在Linux上,事情变得更加复杂,因为真正的操作系统可以保护用户免受键盘记录器的攻击[*].因此,在这里,您将需要一个根进程来监视/dev/input/中的[]句柄之一.最好的选择可能是在/dev/input/by-path/下寻找一个包含字符串"kbd""keyboard"的条目.在大多数情况下应该可以使用.

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.

[*]:哎呀,当我启动一个与键盘事件相关的Python程序时,连我的病毒/特洛伊木马扫描程序都不会抱怨...

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

这篇关于是否有跨平台的python低级API来捕获或生成键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆