【问题标题】:Direct Input command not working while making gamebot制作游戏机器人时直接输入命令不起作用
【发布时间】:2019-11-12 17:17:39
【问题描述】:

我一直致力于使用 python 自动化 GTA V。 为了提供输入,我尝试了“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 上运行它!?

【问题讨论】:

    标签: python machine-learning ctypes directinput scancodes


    【解决方案1】:

    解决方案: 使用管理员权限运行编辑器(在我的例子中是 PyCharm),一切顺利!

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2020-11-27
      • 2017-06-30
      • 2018-03-14
      • 2020-10-05
      • 2020-11-21
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      相关资源
      最近更新 更多