当 python 程序不在前台时,如何读取击键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1054380/
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 21:22:13  来源:igfitidea点击:

How can you read keystrokes when the python program isn't in the foreground?

pythonkeyboardbackgroundkeylogger

提问by Paul Tarjan

I'm trying to analyze my keystrokes over the next month and would like to throw together a simple program to do so. I don't want to exactly log the commands but simply generate general statistics on my key presses.

我正在尝试分析下个月的击键情况,并希望编写一个简单的程序来执行此操作。我不想准确记录命令,而只是简单地生成关于我的按键的一般统计信息。

I am the most comfortable coding this in python, but am open to other suggestions. Is this possible, and if so what python modules should I look at? Has this already been done?

我最喜欢用 python 编码,但我愿意接受其他建议。这可能吗,如果可以,我应该查看哪些 Python 模块?这已经完成了吗?

I'm on OSX but would also be interested in doing this on an Ubuntu box and Windows XP.

我在 OSX 上,但也有兴趣在 Ubuntu 机器和 Windows XP 上执行此操作。

采纳答案by joeforker

It looks like you need http://patorjk.com/keyboard-layout-analyzer/

看起来你需要http://patorjk.com/keyboard-layout-analyzer/

This handy program will analyze a block of text and tell you how far your fingers had to travel to type it, then recommend your optimal layout.

这个方便的程序将分析一段文本并告诉你你的手指必须移动多远才能输入它,然后推荐你的最佳布局。

To answer your original question, on Linux you can read from /dev/event* for local keyboard, mouse and joystick events. I believe you could for example simply cat /dev/event0 > keylogger. The events are instances of struct input_event. See also http://www.linuxjournal.com/article/6429.

要回答您最初的问题,在 Linux 上,您可以从 /dev/event* 读取本地键盘、鼠标和操纵杆事件。我相信你可以简单地举例cat /dev/event0 > keylogger。事件是 的实例struct input_event。另请参阅http://www.linuxjournal.com/article/6429

Python's structmodule is a convenient way to parse binary data.

Python 的struct模块是一种解析二进制数据的便捷方式。

For OSX, take a look at the source code to logkext. http://code.google.com/p/logkext/

对于 OSX,请查看 logkext 的源代码。http://code.google.com/p/logkext/

回答by mzuther

As the current X server's Recordextension seems to be broken, using pykeyloggerfor Linux doesn't really help. Take a look at evdevand its demofunction, instead. The solution is nastier, but it does at least work.

由于当前 X 服务器的Record扩展似乎已损坏,因此pykeylogger用于 Linux 并没有真正的帮助。看看evdev它的demo功能,而不是。解决方案更糟糕,但至少有效。

It comes down to setting up a hook to the device

归结为为设备设置一个钩子

import evdev
keyboard_location = '/dev/input/event1'  # get the correct one from HAL or so
keyboard_device = evdev.Device(keyboard_location)

Then, regularly poll the device to get the status of keys and other information:

然后,定期轮询设备以获取密钥的状态和其他信息:

keyboard_device.poll()

回答by ire_and_curses

Unless you are planning on writing the interfaces yourself, you are going to require some library, since as other posters have pointed out, you need to access low-level key press events managed by the desktop environment.

除非您打算自己编写界面,否则您将需要一些库,因为正如其他海报指出的那样,您需要访问由桌面环境管理的低级按键事件。

On Windows, the PyHooklibrary would give you the functionality you need.

在 Windows 上,PyHook库将为您提供所需的功能。

On Linux, you can use the Python X Library(assuming you are running a graphical desktop).

在 Linux 上,您可以使用Python X 库(假设您正在运行图形桌面)。

Both of these are used to good effect by pykeylogger. You'd be best off downloading the source (see e.g. pyxhook.py) to see specific examples of how key press events are captured. It should be trivial to modify this to sum the distribution of keys rather than recording the ordering.

这两个都被pykeylogger使用,效果很好。您最好下载源代码(参见例如 pyxhook.py)以查看有关如何捕获按键事件的具体示例。修改它以总结键的分布而不是记录排序应该是微不足道的。

回答by hlovdal

Depending on what statistics you want to collect, maybe you do not have to write this yourself; the program Workraveis a program to remind you to take small breaks and does so by monitoring keyboard and mouse activity. It keeps statistics of this activity which you probably could use (unless you want very detailed/more specific statistics). In worst case you could look at the source (C++) to find how it is done.

根据您要收集的统计信息,也许您不必自己编写;Workrave程序是一个通过监视键盘和鼠标活动来提醒您小憩的程序。它保留您可能可以使用的此活动的统计信息(除非您想要非常详细/更具体的统计信息)。在最坏的情况下,您可以查看源代码 (C++) 以了解它是如何完成的。