我一直在使用python自动化GTAV。 为了提供输入,我尝试了“ pyautogui”,但是没有按预期工作。我用谷歌搜索并在stackoverflow上得到了这个解决方案(谢谢,Sentdex!):
Simulate Python keypresses for controlling a game
我使用了'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上运行它??
答案 0 :(得分:0)
解决方案: 以管理员权限运行编辑器(在我的情况下为PyCharm),一切顺利!