制作游戏机器人时直接输入命令不起作用
Direct Input command not working while making gamebot
我一直致力于使用 python 自动化 GTA V。
为了提供输入,我尝试了 "pyautogui" 但它没有按预期工作。我用谷歌搜索并在 Whosebug 上找到了这个解决方案(谢谢你,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 上 运行 这个!?
解决方案:
运行 编辑器(在我的例子中,PyCharm)具有管理员权限,您可以开始了!
我一直致力于使用 python 自动化 GTA V。 为了提供输入,我尝试了 "pyautogui" 但它没有按预期工作。我用谷歌搜索并在 Whosebug 上找到了这个解决方案(谢谢你,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 上 运行 这个!?
解决方案: 运行 编辑器(在我的例子中,PyCharm)具有管理员权限,您可以开始了!