将输入发送到后台运行的Bluestack应用程序

2024-05-16 02:28:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试建立一个简单的机器人,它会定期在游戏聊天中向公会发送邀请。 在Bluestack中开发脚本是一个简单的部分,现在我正在尝试开发一个简单的python程序,通过向Bluestack发送输入来触发这些脚本。 使用Spy++我发现该应用程序包含多个窗口,并具有以下结构:

[-] Window 00010912 "InviteBot" HwndWrapper[Bluestacks.exe;;<a long hex code>]
    [-] Window 00030932 "BlueStacks Android PluginAndroid_1" WindowsForms10.Window.8.app.0.1<hex part>
        [] Window 000409D0 "" WindowsForms10.EDIT.app.0.1<same hex part as above>
        [] Window 0002092E "_ctl.Window" BlueStacksApp

使用Spy++的“查找窗口”功能,我找到了最后一层--“u ctl.Window”

在谷歌搜索之后,我发现了两种向应用程序发送输入的方法(在StackOverflow上),第一种:

wsh = comclt.Dispatch("WScript.Shell")
wsh.AppActivate("InviteBot")  # select another application
wsh.SendKeys("q")  # send the keys you want

工作正常,但激活了窗口,当它发送输入时,很难在PC上工作,因此我需要另一种方法:

def enum_handler(hwnd, lparam):
    if win32gui.IsWindowVisible(hwnd):
        if 'InviteBot' in win32gui.GetWindowText(hwnd):
            invite_bot_handle = hwnd
            print("invite_bot_handle: {}".format(invite_bot_handle))
            print(win32gui.GetWindowText(invite_bot_handle))
            win32gui.PostMessage(invite_bot_handle, win32con.WM_CHAR, 'q', 0)
            win32gui.PostMessage(invite_bot_handle, win32con.WM_KEYDOWN, win32con.VK_F1, 0)
            win32gui.PostMessage(invite_bot_handle, win32con.WM_KEYUP, win32con.VK_F1, 0)

            blue_stacks_app_handle = win32gui.FindWindowEx(invite_bot_handle, None, None, "BlueStacks Android PluginAndroid_1")
            print("blue_stacks_app_handle: {}".format(blue_stacks_app_handle))
            print(win32gui.GetWindowText(blue_stacks_app_handle))
            win32gui.PostMessage(blue_stacks_app_handle, win32con.WM_CHAR, 'q', 0)
            win32gui.PostMessage(blue_stacks_app_handle, win32con.WM_KEYDOWN, win32con.VK_F1, 0)
            win32gui.PostMessage(blue_stacks_app_handle, win32con.WM_KEYUP, win32con.VK_F1, 0)

            target_window_handle = win32gui.FindWindowEx(blue_stacks_app_handle, None, None, "_ctl.Window")
            print("blue_stacks_app_handle: {}".format(target_window_handle))
            print(win32gui.GetWindowText(target_window_handle))
            win32gui.PostMessage(target_window_handle, win32con.WM_CHAR, 'q', 0)
            win32gui.PostMessage(target_window_handle, win32con.WM_KEYDOWN, win32con.VK_F1, 0)
            win32gui.PostMessage(target_window_handle, win32con.WM_KEYUP, win32con.VK_F1, 0)


win32gui.EnumWindows(enum_handler, None)

我尝试将各种类型的输入发送到该继承人统治的所有层,但这些消息似乎没有收到

当我试着打电话的时候 win32gui.MoveWindow(TargetWindowHandle, 0, 0, 760, 500, True) 只是为了确保窗口手柄是我要找的那个,它工作得很好。将此调用为顶级窗口移动了整个BlueStacks应用程序。对于其他层,它只是导致窗口看起来很奇怪。因此句柄值应该是正确的

输出示例(从PyCharm执行)

>>> runfile('D:/Codes/DeffclanRose/BlueStackActions.py', wdir='D:/Codes/DeffclanRose')
invite_bot_handle: 67858
InviteBot
blue_stacks_app_handle: 198962
BlueStacks Android PluginAndroid_1
blue_stacks_app_handle: 133422
_ctl.Window

编辑:我正在寻找的是一种将输入发送到后台运行的应用程序的方法


Tags: apptargetbotbluewindowinvitevkf1
1条回答
网友
1楼 · 发布于 2024-05-16 02:28:30

当窗口处于非活动状态时,Bluestack游戏控制将被禁用,这就是为什么输入在游戏中不起作用。我也有同样的问题,我在发送输入之前使用这个WM_ACTIVATE修复了它:我在这里找到它:https://stackoverflow.com/a/45496600/11915042

以下是我的示例代码:

import win32gui, win32api, win32con
import time

hwnd = win32gui.FindWindow(None, 'BlueStacks')
hwndChild = win32gui.GetWindow(hwnd, win32con.GW_CHILD)
hwndChild2 = win32gui.GetWindow(hwndChild, win32con.GW_CHILD)
 
win32gui.SendMessage(hwnd, win32con.WM_ACTIVATE, win32con.WA_CLICKACTIVE, 0)

time.sleep(1) # Without this delay, inputs are not executing in my case

win32api.PostMessage(hwndChild2, win32con.WM_KEYDOWN, win32con.VK_F1, 0)
time.sleep(.5)
win32api.PostMessage(hwndChild2, win32con.WM_KEYUP, win32con.VK_F1, 0)

相关问题 更多 >