如何在Linux上的后台使用python捕获mouseevents和keyevents [英] How can I capture mouseevents and keyevents using python in background on linux

查看:118
本文介绍了如何在Linux上的后台使用python捕获mouseevents和keyevents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个可以在后台运行但在发生mouseevent或keyevent时打印文本的python脚本.是否有任何库/内置功能可实现此目的?或我可以调用的任何系统命令来获取此信息?成为根没问题.

I'd like to make a python script that can run in the background but print text when a mouseevent or keyevent happens. Are there any libraries/builtin functionality to achieve this? Or any system commands I can call to get this info? Being root is no issue.

推荐答案

我想,您可能会为evdev使用python绑定: http://packages.python.org/evdev/index.html .在教程中,他们为键盘提供了一个示例,但是对于鼠标事件,它应该是类似的:

I guess, you might use python bindings for evdev: http://packages.python.org/evdev/index.html. In tutorial they give an example for keyboard, but it should be similar for mouse events:

>>> from evdev import InputDevice, categorize, ecodes
>>> from select import select
>>> dev = InputDevice('/dev/input/event1')

>>> print(dev)
device /dev/input/event1, name "Dell Dell USB Keyboard", phys "usb-0000:00:12.1-2/input0"

>>> while True:
...    r,w,x = select([dev], [], [])
...    for event in dev.read():
...        if event.type == ecodes.EV_KEY:
...            print(categorize(event))
... # hitting a and holding space
key event at 1337016188.396030, 30 (KEY_A), down
key event at 1337016188.492033, 30 (KEY_A), up
key event at 1337016189.772129, 57 (KEY_SPACE), down
key event at 1337016190.275396, 57 (KEY_SPACE), hold
key event at 1337016190.284160, 57 (KEY_SPACE), up

这篇关于如何在Linux上的后台使用python捕获mouseevents和keyevents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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