简体   繁体   English

直接输入命令在制作Gamebot时不起作用

[英]Direct Input command not working while making gamebot

I've been working on automating GTA V with python. 我一直在使用python自动化GTAV。 To give inputs, I tried "pyautogui" but it wasn't working as expected. 为了提供输入,我尝试了“ pyautogui”,但是没有按预期工作。 I googled and got this solution on stackoverflow (and thank you, Sentdex!): 我用谷歌搜索并在stackoverflow上得到了这个解决方案(谢谢,Sentdex!):

Simulate Python keypresses for controlling a game 模拟Python按键来控制游戏

I used the solution given by 'Hodka', made a few changes (just like sentdex) and here's my following code.. 我使用了“ 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)

and then performing the action... 然后执行操作...

from GameScreen import game_screen, countdown
from Actions import PressKey, ReleaseKey, w
import time


countdown()

print("forward")
PressKey(w)
time.sleep(3)
PressKey(w)

I ran this trial code on GTA V and the player didn't move an inch. 我在GTA V上运行了此试用代码,但播放器没有移动一英寸。 I then tried this code on GTA Vice City and one another game, and the player took forward steps indefinitely (because of the code, but at least it worked there.) I don't understand how does the same code runs in one game but not in another? 然后,我在GTA Vice City和另一款游戏上尝试了此代码,并且玩家无限期地前进了一些步骤(由于有此代码,但至少在那儿有效)。我不知道同一代码如何在一款游戏中运行,但是不在另一个? Save me. 救我。 How do I run this on GTA V!? 如何在GTA V!上运行它?

解决方案:以管理员权限运行编辑器(在我的情况下为PyCharm),一切顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM