我想创建一个可以在后台运行的
python脚本,但是当mouseevent或keyevent发生时打印文本.是否有任何库/内置功能来实现这一目标?或者我可以调用任何系统命令来获取此信息?成为根本不是问题.
解决方法
我想,你可能会使用python绑定evdev:
http://packages.python.org/evdev/index.html.在教程中他们给出了一个键盘示例,但它应该与鼠标事件类似:
>>> 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,up key event at 1337016189.772129,57 (KEY_SPACE),down key event at 1337016190.275396,hold key event at 1337016190.284160,up