# -*- coding: utf-8 -*- """ 获取鼠标点击位置的坐标 使用方法:运行脚本后,点击需要获取坐标的位置,程序会显示坐标 按 ESC 键退出 """ import os import sys import time import ctypes from ctypes import wintypes # 设置控制台编码 if sys.platform == 'win32': os.system('chcp 65001 > nul') # 获取鼠标位置的函数 def get_mouse_position(): class POINT(ctypes.Structure): _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)] pt = POINT() ctypes.windll.user32.GetCursorPos(ctypes.byref(pt)) return pt.x, pt.y # 监听键盘事件 def is_esc_pressed(): if ctypes.windll.user32.GetAsyncKeyState(0x1B) & 0x8000: return True return False def main(): print("=" * 60) print("获取鼠标点击位置的坐标") print("=" * 60) print() print("请依次点击以下位置(按 ESC 键退出):") print("1. 搜索框位置(点击搜索框内)") print("2. 搜索结果区域(点击第一个联系人)") print("3. 聊天输入框位置") print() print("坐标记录:") print("-" * 60) positions = [] labels = ["搜索框", "搜索结果区域", "聊天输入框"] for i, label in enumerate(labels): print(f"\n请点击【{label}】...") time.sleep(0.5) # 等待用户点击 clicked = False while not clicked: if is_esc_pressed(): print("\n\n用户退出") return # 检测鼠标左键按下 if ctypes.windll.user32.GetAsyncKeyState(0x01) & 0x8000: time.sleep(0.2) # 防止抖动 x, y = get_mouse_position() positions.append((label, x, y)) print(f" {label}: ({x}, {y})") clicked = True time.sleep(0.5) # 防止重复触发 time.sleep(0.05) print("\n" + "=" * 60) print("所有坐标获取完成!") print("=" * 60) print() print("坐标汇总:") print("-" * 60) for label, x, y in positions: print(f"{label}: ({x}, {y})") print() # 生成代码片段 print("=" * 60) print("可用于代码的坐标变量:") print("=" * 60) print() print("```python") for label, x, y in positions: # 将标签转换为变量名 var_name = label.replace("搜索框", "search_box").replace("搜索结果区域", "search_result").replace("聊天输入框", "chat_input") print(f"{var_name}_x = {x}") print(f"{var_name}_y = {y}") print("```") # 保存到文件 output_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'wechat_coords.txt') with open(output_file, 'w', encoding='utf-8') as f: f.write("微信关键坐标位置\n") f.write("=" * 50 + "\n\n") for label, x, y in positions: f.write(f"{label}: ({x}, {y})\n") f.write("\n获取时间: " + time.strftime("%Y-%m-%d %H:%M:%S") + "\n") print(f"\n坐标已保存到: {output_file}") if __name__ == '__main__': main()