Simulate key presses in Age of Empires 3

隐身守侯 提交于 2020-08-27 22:14:26

问题


I've been trying to make a simple python script that will send keystrokes to the game Age of Empires 3. I've tried pyautogui as well as using ScanCodes to do DirectInput, but nothing seems to work I just don't understand what kind of input the game needs or how its avoiding getting any inputs from my code. My codes kind of a mess from me testing several different methods, but here it is if anyone wants to see it.

import pyautogui
import pyperclip
import random
import keyboard
import time
from directkeys import PressKey, W, A, S, D


def spawn_hotdog():
    command = "mustard relish and burning oil"
    pyperclip.copy(command)
    pyautogui.click(500, 500)
    pyautogui.keyDown('enter')
    pyautogui.hotkey("ctrlleft", "v")
    pyautogui.keyDown('enter')


loop = False

while True:
    try:
        if keyboard.is_pressed('h'):
            loop = True
        elif keyboard.is_pressed('p'):
            loop = False
        elif keyboard.is_pressed('esc'):
            break
    except:
        pass
    if loop:
        time.sleep(1)
        PressKey(W)
        PressKey(0x1C)

and here is the directkeys.py file which I got from https://pythonprogramming.net/direct-input-game-python-plays-gta-v/

# direct inputs
# source to this solution and code:
# http://stackoverflow.com/questions/14489013/simulate-python-keypresses-for-controlling-a-game
# http://www.gamespp.com/directx/directInputKeyboardScanCodes.html

import ctypes
import time

SendInput = ctypes.windll.user32.SendInput


W = 0x11
A = 0x1E
S = 0x1F
D = 0x20

# 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))

if __name__ == '__main__':
    PressKey(0x11)
    time.sleep(1)
    ReleaseKey(0x11)
    time.sleep(1)`enter code here`

回答1:


The problem was that your code was really buggy that's why it didn't work. I fixed some bug and it works. try to add some keystroke that enables the code.

import pyautogui
import random
import keyboard
import time

def spawn_hotdog():
    time.sleep(5)
    command = "mustard relish and burning oil"#command to type
    pyperclip.copy(command)
    pyautogui.click(500, 500)
    pyautogui.keyDown('enter')
    pyautogui.typewrite(command,interval = 0.10)
    pyautogui.keyUp('enter')#your code was buggy here
#types command in five seconds
spawn_hotdog()

#types command every five seconds
while True:
    spawn_hotdog()
    #add condition to break here!

#the rest of your code doesnt make sense


来源:https://stackoverflow.com/questions/52842667/simulate-key-presses-in-age-of-empires-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!