我正在做一个机器学习项目,试图将输入发送到仿真器以使用python脚本对其进行控制。 我目前正在使用2种不同的Android模拟器a:Memu和b:Tencent Buddy。我的脚本在Memu上运行良好,但根本无法将输入发送给腾讯好友。请帮助我如何解决此问题。
这是我的代码:
test.py
from directkeys import PressKey,ReleaseKey
from keys import KEY_W as W,KEY_A as A,KEY_S as S,KEY_D as D
import time
def sleeper():
for i in range(0,5):
print(i+1)
time.sleep(1)
sleeper()
print('W down')
PressKey(W)
PressKey(A)
time.sleep(.5*60)
print('W up')
ReleaseKey(W)
ReleaseKey(A)
directkeys.py
import ctypes
import time
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)]
# 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)
keys.py
KEY_MOD_LCTRL = 0x1D
KEY_MOD_LSHIFT = 0x2A
KEY_MOD_LALT = 0x38
KEY_CAPSLOCK = 0x3A # Keyboard Caps Lock
KEY_NONE = 0x00
KEY_A = 0x1E # Keyboard a and A
KEY_B = 0x30 # Keyboard b and B
KEY_C = 0x2E # Keyboard c and C
KEY_D = 0x20 # Keyboard d and D
KEY_E = 0x12 # Keyboard e and E
KEY_F = 0x21 # Keyboard f and F
KEY_G = 0x22 # Keyboard g and G
KEY_H = 0x23 # Keyboard h and H
KEY_I = 0x17 # Keyboard i and I
KEY_J = 0x24 # Keyboard j and J
KEY_K = 0x25 # KeyboardK andK
KEY_L = 0x26 # Keyboard l and L
KEY_M = 0x32 # Keyboard m and M
KEY_N = 0x31 # Keyboard n and N
KEY_O = 0x18 # Keyboard o and O
KEY_P = 0x19 # Keyboard p and P
KEY_Q = 0x10 # Keyboard q and Q
KEY_R = 0x13 # Keyboard r and R
KEY_S = 0x1F # Keyboard s and S
KEY_T = 0x14 # Keyboard t and T
KEY_U = 0x16 # Keyboard u and U
KEY_V = 0x2F # Keyboard v and V
KEY_W = 0x11 # Keyboard w and W
KEY_X = 0x2D # Keyboard x and X
KEY_Y = 0x15 # Keyboard y and Y
KEY_Z = 0x2C # Keyboard z and Z
KEY_1 = 0x02 # Keyboard 1 and !
KEY_2 = 0x03 # Keyboard 2 and @
KEY_3 = 0x04 # Keyboard 3 and #
KEY_4 = 0x05 # Keyboard 4 and $
KEY_5 = 0x06 # Keyboard 5 and %
KEY_6 = 0x07 # Keyboard 6 and ^
KEY_7 = 0x08 # Keyboard 7 and &
KEY_8 = 0x09 # Keyboard 8 and *
KEY_9 = 0x0A # Keyboard 9 and (
KEY_0 = 0x0B # Keyboard 0 and )
KEY_ENTER = 0x1C # Keyboard Return (ENTER)
KEY_ESC = 0x01 # Keyboard ESCAPE
KEY_BACKSPACE = 0x0E # Keyboard DELETE (Backspace)
KEY_TAB = 0x0F # Keyboard Tab
KEY_SPACE = 0x39 # Keyboard Spacebar
KEY_MINUS = 0x0C # Keyboard - and _
KEY_EQUAL = 0x0D # Keyboard = and +
KEY_LEFTBRACE = 0x1A # Keyboard [ and {
KEY_RIGHTBRACE = 0x1B # Keyboard ] and }
KEY_BACKSLASH = 0x2B # Keyboard \ and |
KEY_TILDE = 0x29 # Keyboard Non-US # and ~
KEY_SEMICOLON = 0x27 # Keyboard ; and :
KEY_APOSTROPHE = 0x28 # Keyboard ' and "
KEY_GRAVE = 0x29 # Keyboard ` and ~
KEY_COMMA = 0x33 # Keyboard , and <
KEY_DOT = 0x34 # Keyboard . and >
KEY_SLASH = 0x35 # Keyboard / and ?
KEY_F1 = 0x3B # Keyboard F1
KEY_F2 = 0x3C # Keyboard F2
KEY_F3 = 0x3D # Keyboard F3
KEY_F4 = 0x3E # Keyboard F4
KEY_F5 = 0x3F # Keyboard F5
KEY_F6 = 0x40 # Keyboard F6
KEY_F7 = 0x41 # Keyboard F7
KEY_F8 = 0x42 # Keyboard F8
KEY_F9 = 0x43 # Keyboard F9
KEY_F10 = 0x44 # Keyboard F10
KEY_F11 = 0x85 # Keyboard F11
KEY_F12 = 0x86 # Keyboard F12
KEY_KP1 = 0x4F # Keypad 1 and End
KEY_KP2 = 0x50 # Keypad 2 and Down Arrow
KEY_KP3 = 0x51 # Keypad 3 and PageDn
KEY_KP4 = 0x4B # Keypad 4 and Left Arrow
KEY_KP5 = 0x4C # Keypad 5
KEY_KP6 = 0x4D # Keypad 6 and Right Arrow
KEY_KP7 = 0x47 # Keypad 7 and Home
KEY_KP8 = 0x48 # Keypad 8 and Up Arrow
KEY_KP9 = 0x49 # Keypad 9 and Page Up
KEY_KP0 = 0x52 # Keypad 0 and Insert
KEY_KPDOT = 0x53 # Keypad . and Delete
使用腾讯伙伴对我来说非常重要,因为它的键映射比memu更好,因为机器学习脚本很容易。
我也有一个关于如何使鼠标拖动运动以使游戏摄像机移动的问题。这是第二个问题。