如何使用Python控制游戏?

问题描述 投票:0回答:1

我正在尝试通过使用Scancodes使用Python在GTA5中控制车辆。

我已经尝试过使用Scancodes,尽管我在Python Shell和所有基于文本的应用程序上都得到了按键,但是GTA 5拒绝接受输入。

import ctypes
import time

for i in list(range(4)) [::-1]:
    print(i+1)
    time.sleep(1)

SendInput = ctypes.windll.user32.SendInput

W=0x11
S=0x1F
D=0x20
A=0x1E

# C struct redefinitions 
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time",ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]

# Actuals Functions

def PressKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))


if __name__=='__main__':
    while (True):
        PressKey(0x11)
        time.sleep(1)
        ReleaseKey(0x11)
        time.sleep(1)

此代码来自另一个线程Simulate Python keypresses for controlling a game

脚本需要通过自动按下'W'使角色在游戏中前进。

但是我无法在GTA 5上使用它。但是,该视频中使用了相同的代码:-https://www.youtube.com/watch?v=tWqbl9IUdCg&list=PLQVvvaa0QuDeETZEOy4VdocT7TOjfSA8a&index=3,它仍然有效。

python ctypes
1个回答
0
投票

以管理员身份打开cmd,使用cd进入目录并运行脚本。在4秒计时器结束之前快速alt键。它将起作用。

ps:即使我也尝试过同样的方法,但发现了这个问题,尝试了@SyntaxVoid支持Monica并进行了尝试。如果您以管理员身份空闲运行python,它将无法正常工作。您需要使用cmd。

您需要alt-w-tab才能退出gta5

© www.soinside.com 2019 - 2024. All rights reserved.