如何使用pywinauto模拟游戏中的按键 [英] How to simulate keys in game with pywinauto

查看:209
本文介绍了如何使用pywinauto模拟游戏中的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一年来我一直在尝试各种事情.我是python的初学者.在Euler项目中做了前两个问题.

I've been trying various things for a year. I am a at beginner's level in python. Did the first two questions in Project Euler.

我尝试了几种方法来尝试模拟我玩的游戏中的按键.我可以通过自动热键和宏键盘/鼠标轻松地做到这一点.但是,我想通过Python或C来实现.

I have tried a few methods to try and simulate keys in games I play. I can easily do this with autohotkey and macro keyboards/mouse. However, I would like to accomplish this through Python or C.

我的猜测是,视频游戏中将忽略win32 api,我需要通过Direct X模拟按键.

My guess is that win32 api is ignored in video games and I need to simulate key presses through Direct X.

先谢谢您.这是我最近的尝试...失败了.

Thank you in advance. Here is my latest attempt... it failed.

每次运行新的游戏实例时,我都必须抓住/更改手柄.

I do have to grab/change the handle every time I run a new instance of the game.

我的模拟键可以在浏览器和记事本中使用,而不能在游戏中使用.通过不工作,我的意思是没有用户输入.

My simulated keys would work in the browser and notepad, just not in game. By not working, I mean there is no user input.

以下代码将切换到窗口,但不会模拟用户的输入.

The following code would switch to the window but will not simulate an input from the user.

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

推荐答案

我们过去的自我经历了一段漫长的旅程.尽管我们有很多要学习的地方,但我们还是发现了什么:

We have had a long journey my past self. Though we have much to learn here is what we discovered:

如果游戏在DirectX上运行,则发送虚拟键将被忽略.使用sendinput并发送scan_codes.

Sending Virtual Keys will be ignored if the game is running on DirectX. Use sendinput and send the scan_codes instead.

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

来源:模拟Python按键来控制游戏 http://www.gamespp.com/directx/directInputKeyboardScanCodes.html

感谢Sentdex提供有关机器学习的出色教程.我一直想对我最喜欢的一些游戏进行此操作,但由于DirectX的原因,无法获得所有的按键.

Thank you Sentdex for awesome tutorials on Machine Learning. I've been wanting to do this to some of my favorite games but couldn't get the keys across (due to DirectX).

下一步:Windows驱动程序工具包可模拟按键...

Next step: Windows Driver Kit to emulate keypresses...

这篇关于如何使用pywinauto模拟游戏中的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆