import win32gui import pyautogui import pyperclip import time def move_and_paste(hwnd_hex, x_offset, y_offset, text): hwnd = int(hwnd_hex, 16) # 将十六进制句柄转换为十进制 # 获取窗口的矩形 left, top, right, bot = win32gui.GetWindowRect(hwnd) # 计算输入框的绝对位置 input_x = left + x_offset # 水平位置 input_y = bot + y_offset # 垂直位置,相对于窗口底部向上 # 复制前确认剪贴板内容 print("剪贴板内容:", pyperclip.paste()) pyperclip.copy(text) print("已复制的内容:", pyperclip.paste()) # 移动鼠标到输入框位置 pyautogui.moveTo(input_x, input_y, duration=0.5) # 点击鼠标左键以激活输入框 pyautogui.click() time.sleep(1) # 增加等待时间 pyautogui.click() # 双击以确保激活 # 等待一段时间以确保输入框已激活 time.sleep(0.5) # 粘贴文本 pyautogui.hotkey('ctrl', 'v') # 发送回车键以发送消息 pyautogui.press('enter') # 等待一段时间以确保消息已发送 time.sleep(0.5) print("文本已粘贴并发送") # 使用示例 hwnd_hex = '000112C0' # 窗口句柄 x_offset = 20 # 水平位置的像素偏移量,假设输入框位于窗口左边距20像素的位置 y_offset = 100 # 垂直位置的像素偏移量,从窗口底部向上100像素 text = '你好' # 要输入的文本 move_and_paste(hwnd_hex, x_offset, y_offset, text)
获取千牛聊天窗口,以及自动发送消息简易demo
最新推荐文章于 2025-03-25 15:52:37 发布