有没有一种方法可以将点击事件发送到python后台的窗口中? [英] Is there a way to send a click event to a window in the background in python?

查看:174
本文介绍了有没有一种方法可以将点击事件发送到python后台的窗口中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试构建一个机器人,以自动化通过Bluestacks在PC上运行的手机游戏中的某些动作.

So I'm trying to build a bot to automate some actions in a mobile game that I'm running on my pc through Bluestacks.

我的程序获取窗口的屏幕快照,在图像中查找某些按钮模板并返回其坐标.

My program takes a screenshot of the window, looks for certain button templates in the image and returns their coordinates.

我现在希望能够在这些坐标处将click事件发送到窗口,但是由于我也想在机器人在后台运行时执行其他操作,因此我正在寻找一种发送鼠标的方法事件直接传递到窗口(即使已最小化/在后台),而不会影响我做其他事情或将窗口置于前景/使窗口最小化时的鼠标移动.这可能吗?

I would now like to be able to send a click event to the window at those coordinates, but since I would also like to do other things while the bot runs in the background I'm looking for a way to send the mouse event directly to the window (even if it's minimized/in the background), without influencing the movement of the mouse while I'm doing other stuff or bringing the window to the foreground/unminimizing it. Is this possible?

推荐答案

基于Dmitry的回答. x y 必须是相对于Bluestacks窗口而不是屏幕的坐标.

Based on Dmitry's answer. The x and y must be the coordinates relative to the Bluestacks window not the screen.

def click(x, y):
    hWnd = win32gui.FindWindow(None, "BlueStacks")
    lParam = win32api.MAKELONG(x, y)

    hWnd1= win32gui.FindWindowEx(hWnd, None, None, None)
    win32gui.SendMessage(hWnd1, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
    win32gui.SendMessage(hWnd1, win32con.WM_LBUTTONUP, None, lParam)

click(100,100)

这篇关于有没有一种方法可以将点击事件发送到python后台的窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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