You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Minecraft时PyAutoGUI无法主动移动鼠标仅手动晃动时生效如何解决

问题根源

你遇到的是3D游戏输入模拟的常见问题,核心原因是Minecraft采用Raw Input/DirectInput模式接管鼠标输入,PyAutoGUI默认使用的普通系统级鼠标模拟接口不会被游戏捕获,只有手动移动鼠标触发游戏的输入监听后,模拟操作才会被顺带识别。

可行解决方案
  • 更换输入模拟库:替换PyAutoGUI为pydirectinput,该库直接调用DirectInput接口,适配绝大多数3D游戏的输入捕获逻辑。安装命令:pip install pydirectinput,代码中直接将pyautogui替换为pydirectinput即可,API基本完全兼容。
  • 给鼠标移动操作加持续时间:你当前的moveRel是瞬时位移,很容易被游戏的输入轮询漏掉,添加极小的持续时间让移动有可被识别的过程,例如moveRel(0, 500, duration=0.02)。
  • 调整等待时长:你当前的等待时间多为0.01秒,操作间隔太短会导致游戏来不及响应上一步操作,建议将所有等待时间调整到0.05秒以上。
  • 关闭PyAutoGUI故障安全(可选):如果是移动到屏幕边缘触发了故障安全停住,可以在代码开头添加pyautogui.FAILSAFE = False。
修改后的参考代码
import pydirectinput
import time

# 可选,禁用故障安全
pydirectinput.FAILSAFE = False

def mine():
    count = 0
    while not found_diamonds:
        # if keyboard.is_pressed('q'):
            # break
        # pydirectinput.keyDown('w')
        # pydirectinput.mouseDown()
        count += 1
        if count >= 20:
            count = 0
            pydirectinput.mouseUp()
            time.sleep(0.05)
            # 向下转视角,可调整duration和位移数值适配你的游戏灵敏度
            pydirectinput.moveRel(0, 500, duration=0.02)
            time.sleep(0.05)
            pydirectinput.rightClick()
            time.sleep(0.05)
            # 复位视角,位移数值根据实际效果调整
            pydirectinput.moveRel(0, -400, duration=0.02)
            time.sleep(0.05)

内容的提问来源于stack exchange,提问作者TotallyNotTeo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 06:15:08