当前位置: 首页> 游戏> 网游 > python --监听鼠标事件

python --监听鼠标事件

时间:2025/7/14 0:55:38来源:https://blog.csdn.net/weixin_44634704/article/details/139497124 浏览次数:0次
import pyautogui
from pynput import mouse, keyboardpyautogui.FAILSAFE =Falseclass MouseMonitor:def __init__(self):self.mouse = mouse.Controller()self.lock = Truedef on_move(self, x, y):'''鼠标位移时回调函数'''if self.lock:print('11')pyautogui.moveTo(500, 500)self.lock = Falsedef on_click(self, x, y, button, pressed):# 在鼠标点击时被调用# 这里可以添加你想要执行的代码action = "Pressed" if pressed else "Released"print(f"Mouse {action} at ({x}, {y}) with {button}")def on_scroll(self, x, y, dx, dy):# 在鼠标滚动时被调用# 这里可以添加你想要执行的代码print(f"Mouse scrolled at ({x}, {y}) with delta ({dx}, {dy})")def on_press(self, key):# 返回False来拦截按键输入try:print(key.char)except Exception:print(key)# return Falsereturn Truedef start_monitoring(self):# 监听鼠标事件l1 = mouse.Listener(on_move=self.on_move, on_click=self.on_click, on_scroll=self.on_scroll, on_press=self.on_press)l2 = keyboard.Listener(on_press=self.on_press)l1.start()l2.start()l1.join()l2.join()if __name__ == "__main__":mouse_monitor = MouseMonitor()mouse_monitor.start_monitoring()
关键字:python --监听鼠标事件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: