Roblox无法识别PyAutoGUI鼠标移动问题求助
问题
我编写了一个Roblox断连自动重连脚本,通过ctypes读取屏幕像素判断是否触发断连状态,但遇到核心问题:Roblox无法识别PyAutoGUI的鼠标移动与点击操作,手动移动鼠标点击按钮则完全正常。脚本逻辑为:检测到断连标识像素后,等待网络恢复,自动点击重连按钮,但按钮无任何响应。
原代码
import pyautogui as py import keyboard import tkinter import requests from ctypes import windll from time import sleep key = "m" toggled = False rjcolor = 16777215 root = tkinter.Tk() root.withdraw() width, height = root.winfo_screenwidth(), root.winfo_screenheight() dc= windll.user32.GetDC(0) def getpixel(x,y): return windll.gdi32.GetPixel(dc,x,y) while True: if keyboard.is_pressed(key): toggled = not toggled print("toggled to " + str(toggled)) sleep(0.5) if toggled == True: py.moveTo(width / 2, 800) py.click(button='left') if getpixel(1050, 600) == rjcolor: print("disconnected, waiting until internet back online!") while True: try: requests.head("http://www.google.com/", timeout=3) print('The internet connection is active, rejoining.') py.moveTo(1050, 590) py.mouseDown(button='left') sleep(0.1) py.mouseUp(button='left') break except requests.ConnectionError: print("The internet connection is down") sleep(3) sleep(0.1)
问题原因
PyAutoGUI依赖Windows的SendInput API模拟输入,但Roblox等游戏会对上层模拟输入做拦截,优先识别硬件级别的鼠标操作,导致PyAutoGUI的操作无法被游戏捕捉。
解决方案
使用ctypes直接调用Windows底层API模拟原生鼠标输入,绕过游戏的输入拦截。以下是修改后的完整代码,替换了PyAutoGUI的鼠标操作:
import keyboard import tkinter import requests from ctypes import windll, c_uint from time import sleep # Windows API 常量定义 MOUSEEVENTF_MOVE = 0x0001 MOUSEEVENTF_LEFTDOWN = 0x0002 MOUSEEVENTF_LEFTUP = 0x0004 MOUSEEVENTF_ABSOLUTE = 0x8000 # 获取屏幕分辨率 root = tkinter.Tk() root.withdraw() SCREEN_WIDTH = root.winfo_screenwidth() SCREEN_HEIGHT = root.winfo_screenheight() # 初始化Windows API user32 = windll.user32 gdi32 = windll.gdi32 dc = user32.GetDC(0) def getpixel(x, y): return gdi32.GetPixel(dc, x, y) def move_mouse(x, y): # 转换为绝对坐标(0-65535范围,适配Windows API要求) abs_x = int(x * 65535 / SCREEN_WIDTH) abs_y = int(y * 65535 / SCREEN_HEIGHT) user32.mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, abs_x, abs_y, 0, 0) def click_mouse(x, y): move_mouse(x, y) sleep(0.05) # 模拟左键按下+松开 user32.mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) sleep(0.1) user32.mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) # 脚本核心逻辑 key = "m" toggled = False rjcolor = 16777215 while True: if keyboard.is_pressed(key): toggled = not toggled print(f"toggled to {toggled}") sleep(0.5) if toggled: click_mouse(SCREEN_WIDTH / 2, 800) if getpixel(1050, 600) == rjcolor: print("disconnected, waiting until internet back online!") while True: try: requests.head("http://www.google.com/", timeout=3) print('The internet connection is active, rejoining.') click_mouse(1050, 590) break except requests.ConnectionError: print("The internet connection is down") sleep(3) sleep(0.1)
额外注意事项
- 管理员权限运行:右键脚本选择“以管理员身份运行”,避免系统权限限制导致输入模拟失效。
- 坐标校准:确保
1050, 590等坐标与你屏幕上Roblox重连按钮的实际位置一致,可临时用pyautogui.displayMousePosition()获取坐标(仅用于校准,实际运行用修改后的代码)。 - 颜色值验证:确认
rjcolor = 16777215(纯白色)确实是断连状态下的标识像素颜色,避免误触发。
内容的提问来源于stack exchange,提问作者Piggy Gaming
相关产品推荐
相关产品推荐

