我一直致力于使用 python 自动化 GTA V。为了提供输入,我尝试了“pyautogui”,但它没有按预期工作。我用谷歌搜索并在 stackoverflow 上得到了这个解决方案(谢谢你,Sentdex!):
我使用了 'Hodka' 给出的解决方案,做了一些更改(就像 senddex 一样),这是我的以下代码..
import ctypes
import time
SendInput = ctypes.windll.user32.SendInput
w = 0x11
# 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))
# directx scan codes http://www.gamespp.com/directx/directInputKeyboardScanCodes.html
if __name__ == '__main__':
while (True):
PressKey(0x11)
time.sleep(1)
ReleaseKey(0x11)
time.sleep(1)
然后执行动作...
from GameScreen import game_screen, countdown
from Actions import PressKey, ReleaseKey, w
import time
countdown()
print("forward")
PressKey(w)
time.sleep(3)
PressKey(w)
我在 GTA V 上运行了这个试用代码,玩家一动不动。然后我在 GTA Vice City 和另一个游戏上尝试了这个代码,玩家无限期地向前迈进了一步(因为代码,但至少它在那里工作。)我不明白相同的代码是如何在一个游戏中运行的,但是不在另一个?救我。如何在 GTA V 上运行它!?