赞
踩
当界面不在激活状态时,想要获取鼠标键盘的操作情况,没有什么其他的选择,pynput是一个不错的第三方库。
关于pynput的用法网上有很多程序的示例,不过其中的Listener.join方法会造成阻塞,并且在监听键盘的同时无法监听鼠标。
并且pynput的程序设计,需要对键盘按下/键盘抬起/鼠标移动/鼠标点击/滚轮滚动5个事件分别分配回调函数,非常不“Pythonic”。
所以做了一个简单的小轮子,使用后台多线程的方法,将键盘/鼠标的监控线程在后台发起,并支持退出;并且通过将待调用函数方法传入,将控制逻辑写在监控器外面,做到逻辑和功能隔离。
Talk is cheap, show the code。代码自带展示界面,直接运行即可测试效果:
import tkinter from threading import Thread import pynput class Monitor: def __init__(self, func): self.func = func self.kl = self.ml = None def start(self): self.stop() Thread(target=self.mouser).start() Thread(target=self.keyboarder).start() def hook(self, *typ_names): for name in typ_names: yield (lambda typ: (lambda *args: self.func(typ, *args)))(name) def mouser(self): with pynput.mouse.Listener(*self.hook('move', 'click', 'scroll')) as self.ml: self.ml.join() def keyboarder(self): with pynput.keyboard.Listener(*self.hook('press', 'release')) as self.kl: self.kl.join() def stop(self): if self.kl or self.ml: pynput.keyboard.Listener.stop(self.kl) pynput.mouse.Listener.stop(self.ml) def SetLabel(*args): label.config(text=','.join(map(str, args))) # monitor = Monitor(print) monitor = Monitor(SetLabel) top = tkinter.Tk() label = tkinter.Label(top, width=30, text='initial text here') tkinter.Button(top, text='开始线程', command=monitor.start).pack(side='left') tkinter.Button(top, text='停止线程', command=monitor.stop).pack(side='left') label.pack(side='left') top.mainloop()
如果监控器只需要初始化启动和最后的退出,不需要多次停止和重启,Monitor类还可以写得再简单一点, 当然,对应的“开始线程”控制函数也要对应删除:
class Monitor: def __init__(self, func): self.func = func Thread(target=self.mouser).start() Thread(target=self.keyboarder).start() def hook(self, *typ_names): for name in typ_names: yield (lambda typ: (lambda *args: self.func(typ, *args)))(name) def mouser(self): with pynput.mouse.Listener(*self.hook('move', 'click', 'scroll')) as self.ml: self.ml.join() def keyboarder(self): with pynput.keyboard.Listener(*self.hook('press', 'release')) as self.kl: self.kl.join() def stop(self): pynput.keyboard.Listener.stop(self.kl) pynput.mouse.Listener.stop(self.ml)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。