如何使用python模拟桌面在(x,y)位置的点击? [英] How to simulate desktop click at (x,y) position with python?

查看:66
本文介绍了如何使用python模拟桌面在(x,y)位置的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单击桌面上的某处,我在Win32 API中使用 python ,我在使用32位python,但是我的计算机是64位计算机.我相信lParam变量没有保存我期望的值,并且我对该变量本身仍然有些困惑,可以说我是从wintypes导入的,有人可以告诉我如何使用它吗?为什么我下面的功能不起作用?

I'm trying to click somewhere on the desktop, I'm using python with win32 api, I'm using python 32 bit but my computer is a 64 bit computer. I believe the lParam variable isn't holding the value I'm expecting, and I'm still a bit confused about this variable itself, lets say I import it from wintypes can anyone tell me how to use it? Why does my function below not work?

我有一个如下功能,这似乎不起作用:

I have a function as following, this doesn't seem to work:

def clickDesktop(x=0, y=0):

    # Get handle to desktop window
    desktop = win32gui.GetDesktopWindow()

    # Create variable lParam that contains the x-coordinate in  the low-order word while
    # the high-order word contains the y coordinate.
    lParam = y << 16 | x

    # Click at x, y in the desktop window
    win32gui.PostMessage(desktop, win32con.WM_LBUTTONDOWN, MK_LBUTTON, lParam)
    win32gui.PostMessage(desktop, win32con.WM_LBUTTONUP, 0, lParam)

推荐答案

以下代码在Windows 7上适用于Python33.

The following code works with Python33 on Windows 7.

我使用了 ctypes .

WM_LBUTTONDBLCLK LPARAM 参数将x和y组合为单个32位值.

The LPARAM parameter for WM_LBUTTONDBLCLK combines x and y in a single 32 bits value.

运行该代码时,它将打开位于桌面左上角的我的电脑"图标(我的任务栏也在左侧,因此x的高值110).

When I run that code, it opens the "My Computer" Icon, located at the upper left corner of my Desktop (my TaskBar is also on the left, hence the high value of 110 for x).

from ctypes import windll

WM_LBUTTONDBLCLK = 0x0203
MK_LBUTTON = 0x0001

if __name__=='__main__':
    hProgman = windll.User32.FindWindowW( "Progman", 0 )
    if hProgman != 0:
        hFolder = windll.User32.FindWindowExW( hProgman, 0, "SHELLDLL_DefView", 0 )
        if hFolder != 0:
            hListView = windll.User32.FindWindowExW( hFolder, 0, "SysListView32", 0 )
            if hListView != 0:
                windll.User32.PostMessageW( hListView, WM_LBUTTONDBLCLK, MK_LBUTTON,
                                            110 + (65536*32) )

编辑Windows通常将 WM_LBUTTON * 消息发布到指针下方的窗口中.桌面窗口有 子窗口,这些窗口是在指针下方"的子窗口.如果您想使用PostMessage API,则需要知道将消息发布到哪个窗口.

EDIT the WM_LBUTTON* messages are normally posted by Windows to the window under the pointer. The desktop window has child windows, and that's those child windows which are "under the pointer". If you want to use the PostMessage API, you need to know to what window you will post the message.

如果您不想打扰Windows层次结构,只需使用 SendInput .然后,Window将为您完成工作,最后将鼠标消息发布到正确的句柄.

If you don't want to bother with windows hierarchy, the just use SendInput. Window will then do the work for you and finally post the mouse message to the correct handle.

这篇关于如何使用python模拟桌面在(x,y)位置的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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