# mouse_logger.py
from pynput import keyboard, mouse
import time
# 存储坐标
coordinates = []
# 获取鼠标控制器实例(用于获取当前位置)
mouse_controller = mouse.Controller()
def on_press(key):
try:
if key.char == 'y': # 按下 y 键
x, y = mouse_controller.position
coordinates.append((x, y))
print(f"✅ 记录坐标: ({x}, {y})")
except AttributeError:
# 忽略特殊键(如 Ctrl, Alt 等)
pass
def on_release(key):
if key == keyboard.Key.esc:
return False # 停止监听(可选)
# 开始监听键盘
listener = keyboard.Listener(on_press=on_press)
listener.start()
print("🖱️ 鼠标坐标记录器已启动")
print("📌 按下 'y' 键记录当前鼠标位置")
print("🛑 按 Ctrl+C 退出并保存数据")
try:
while True:
time.sleep(0.01) # 轻量循环,减少CPU占用
except KeyboardInterrupt:
print("\n\n👋 程序已退出,共记录 %d 个坐标:" % len(coordinates))
for i, (x, y) in enumerate(coordinates, 1):
print(f"{i}: ({x}, {y})")
# 可选:保存到文件
with open("mouse_coordinates.txt", "w") as f:
for x, y in coordinates:
f.write(f"{x},{y}\n")
print("💾 坐标已保存到 mouse_coordinates.txt")
一串鼠标位置记录的代码 简单适合初学 点击y键记录一个路径点 基于循环实现持续记录
Python鼠标轨迹记录教程
最新推荐文章于 2025-12-01 21:36:14 发布
16万+

被折叠的 条评论
为什么被折叠?



