python – How to send keyboard commands to emulator?

Question:

Hey Guys, I'm trying to make a program to make an automatic combo in a fighting game (The King of Fighters 2002), I've tried using libraries like pyautogui, keyboard, ctypes and etc, but I can't make the program press the keys to walk automatically in the emulator (Fightcade), can anyone tell me how to solve? follow what I have in code so far:

import pyautogui
import keyboard
import time

def Combo1():
    while True:
        if keyboard.is_pressed('q'):
            pyautogui.keyDown('left')
            time.sleep(0.1)
            pyautogui.keyUp('left')

Combo1()

the one at the bottom is using a windows api to do the commands with direct keys…

import ctypes
import keyboard
from ctypes import wintypes
import time

user32 = ctypes.WinDLL('user32', use_last_error=True)

INPUT_MOUSE    = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2

KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP       = 0x0002
KEYEVENTF_UNICODE     = 0x0004
KEYEVENTF_SCANCODE    = 0x0008

MAPVK_VK_TO_VSC = 0

# msdn.microsoft.com/en-us/library/dd375731

Z = 0x2C
X = 0x2D
C = 0x2E
V = 0x2F

LEFT = 0xCB
RIGHT = 0xCD
UP = 0xC8
DOWN = 0xD0

# C struct definitions

SendInput = ctypes.windll.user32.SendInput

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

def Combo1():
    flag = True
    while True:
        if keyboard.is_pressed('q'):
            while flag: 
                PressKey(DOWN) # 
                time.sleep(0.1)
                ReleaseKey(DOWN) #
                if keyboard.is_pressed('q'):
                    flag = False
            
if __name__ == "__main__":
    Combo1()

Answer:

Look for a tool that calls AutoHotKey . It allows you to write scripts to capture keyboard or mouse input and run actions automatically. You can write a script telling you what commands will be sent when you press a certain key. The language to write the scripts is super simple and you can enable/disable each script when using it.

As an example, here's a script I wrote to replace the CTRL + Alt Gr key combination with a right arrow when in Spyder's window:

#IfWinActive Spyder (Python 3.8)
LControl & RAlt::
Send, {Right}
return
Scroll to Top