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

为什么Python脚本在桌面运行正常但在GTA V等游戏中无法触发输入操作

问题原因
  • 大部分3D游戏(包含GTA V)采用DirectInput接口处理输入信号,pyautogui默认调用的Windows系统标准模拟输入接口会被游戏主动屏蔽,无法生效。
  • 你已经导入了专门适配DirectInput的pydirectinput库,但代码中没有调用,还是使用了pyautogui的输入方法。
  • 全屏模式下游戏画面由DirectX直接渲染,pyautogui的图像识别接口无法捕获对应画面,会导致匹配一直失败。
解决方案
  • 将GTA V显示模式调整为无边框窗口化,保证图像识别功能可以正常捕获画面内容。
  • 右键点击Python终端/IDE,选择以管理员身份运行后再启动脚本,避免权限不足导致输入被系统拦截。
  • 将代码中所有输入相关的pyautogui调用替换为pydirectinput调用,匹配游戏的输入栈要求。
修改后参考代码
import pyautogui
import pydirectinput
import time

# 可选配置:降低pydirectinput的输入延迟,默认有0.1s的防误触延迟
pydirectinput.PAUSE = 0.01

while True:
    riba3 = pyautogui.locateCenterOnScreen('riba3.png', confidence=0.5)
    riba2 = pyautogui.locateCenterOnScreen('riba2.png', confidence=0.5)
    riba1 = pyautogui.locateCenterOnScreen('riba1.png', confidence=0.5)
    if riba3:
        pydirectinput.moveTo(riba3.x, riba3.y)
        pydirectinput.click(clicks=3, interval=1)
    elif riba2:
        pydirectinput.moveTo(riba2.x, riba2.y)
        pydirectinput.click(clicks=3, interval=1)
    elif riba1:
        pydirectinput.moveTo(riba1.x, riba1.y)
        pydirectinput.click(clicks=3, interval=1)
    # 添加短休眠降低CPU占用
    time.sleep(0.1)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 07:36:03