一年来我一直在尝试各种各样的东西。我是蟒蛇的初学者。做了Euler项目中的前两个问题。
我试过几种方法来模拟我玩的游戏中的键。我可以很容易地用自动热键和宏键盘/鼠标来完成这个任务。但是,我想通过Python或C来实现这一点。
我的猜测是,win32 api在游戏中被忽略了,我需要通过Direct模拟按键。
提前谢谢你。这是我最近的尝试..。失败了。
每次我运行新的游戏实例时,我都必须抓取/更改句柄。
我的模拟键可以在浏览器和记事本中工作,只是在游戏中不起作用。不工作,我的意思是没有用户输入。
下面的代码将切换到窗口,但不会模拟用户的输入。
import pywinauto
import time
from pywinauto import application
app = application.Application()
app.connect_(handle = 0x14002a)
dialogs = app.windows_(handle = 0x14002a)
dlg = app.top_window_()
time.sleep(1)
app.MapleStory.TypeKeys("%A")
time.sleep(1)
app.MapleStory.TypeKeys("%A")
time.sleep(1)
app.MapleStory.TypeKeys("%A")
time.sleep(1)
app.MapleStory.TypeKeys("%A")
time.sleep(1)
app.MapleStory.TypeKeys("%A")
time.sleep(1)
app.MapleStory.TypeKeys("%A")发布于 2017-04-27 04:34:11
我们经历了一段漫长的旅程,我过去的自己。虽然我们在这里有很多要学的东西,但我们发现:
如果游戏在DirectX上运行,发送虚拟键将被忽略。使用sendinput并发送scan_codes。
# http://www.gamespp.com/directx/directInputKeyboardScanCodes.html
import ctypes
import time
SendInput = ctypes.windll.user32.SendInput
W = 0x11
A = 0x1E
S = 0x1F
D = 0x20
Z = 0x2C
UP = 0xC8
DOWN = 0xD0
LEFT = 0xCB
RIGHT = 0xCD
ENTER = 0x1C
# 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)资料来源:Simulate Python keypresses for controlling a game http://www.gamespp.com/directx/directInputKeyboardScanCodes.html
感谢Sentdex关于机器学习的精彩教程。我一直想对一些我最喜欢的游戏做这件事,但由于DirectX的原因,我没能把钥匙拿过去。
下一步: Windows驱动程序工具包来模拟按键..。
发布于 2018-10-14 21:45:58
我不知道这是不是太晚了,但是是的,在某些游戏中,你可以用扫描代码来模拟鼠标/键盘事件,而不是用SendInput来模拟虚拟键。
但是,特别是Maplestory,它不响应虚拟密钥和扫描代码。
在这个游戏中模拟键盘事件的唯一可能方法是使用内核驱动程序。一个例子是使用WinIo直接写入i8042和i8048控制器。8042上的命令0xD2专门用于模拟硬件级输入。然而,WinIo直接向用户空间程序公开硬件端口,从而打开了一个很大的安全漏洞,所以我不能说我推荐它。
https://stackoverflow.com/questions/25660685
复制相似问题