pythonsystem

How do I accurately represent the input of any key from the keyboard using Python?


I'm trying to move the character in the game Wuthering waves using Python code. I've tried many libraries, but the character still doesn't move. Like pydirectinput, pynput, ctypes, win32api.

using pydirectinput

import time
import pydirectinput

pydirectinput.FAILSAFE = False

def press_key(key):
    pydirectinput.keyDown(key_dict[key])
    time.sleep(0.1)
    pydirectinput.keyUp(key_dict[key])
    time.sleep(0.1)
while True:
    press_key('a')

using ctypes:

import time
import ctypes

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

def press_key(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput(hexKeyCode, 0x48, 0, 0, ctypes.pointer(extra))
    x = Input(ctypes.c_ulong(1), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def release_key(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput(hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra))
    x = Input(ctypes.c_ulong(1), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

key_dict = {
    'A': 0x1E,
    'SPACE': 0x39,
    'ENTER': 0x1C,
    'UP': 0xC8,
    'DOWN': 0xD0,
    'LEFT': 0xCB,
    'RIGHT': 0xCD
}

while True:
    press_key(key_dict['A'])
    time.sleep(0.1)
    release_key(key_dict['A'])
    time.sleep(0.1)

I would like to have Python code that can help me to move the character in the game.


Solution

  • The problem is very probably not on the script itself.

    Wuthering Waves uses ACE Anti-cheat and other complimentary measures to stop macros from being used.

    An easier way out would probably be to use an emulator, such as Bluestacks and simulate the inputs on the emulator, which would not be an input directly to the game, and would therefore be not so easy to detect.

    Be careful, though. I wouldn't directly recommend doing this since you could still easily get banned if the anti-cheat detects any suspicious behavior (which it is constantly monitoring for).