2

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)
TheCodingGent
  • 110
  • 2
  • 10
  • You should use the SendInput Win32 API. – manuell Feb 05 '14 at 20:13
  • What's displayed where you want to click? – manuell Feb 05 '14 at 21:50
  • use ctypes for calling SendInput. See for example http://stackoverflow.com/questions/19288656/python-sendinput-not-working-on-remote-desktop-after-disconnection for faking keyboard. – manuell Feb 05 '14 at 21:52
  • This doesn't work either I don't see anything happening.. – TheCodingGent Feb 06 '14 at 14:37
  • What's displayed where you want to click? – manuell Feb 06 '14 at 14:55
  • I'm currently clicking an icon. But to me it doesn't matter what I'm clicking the idea is to be able to bring the desktop to focus and send a click to it at a specific position. – TheCodingGent Feb 06 '14 at 15:07
  • What version of Windows? – manuell Feb 06 '14 at 17:09
  • possible duplicate of [python win32 simulate click](http://stackoverflow.com/questions/2964051/python-win32-simulate-click) – iljau Feb 06 '14 at 20:09
  • the question is similar but the answer is still not what I'm looking for because I'm trying to send a click message to specific window (in this case the desktop) and not click on the screen at an absolute position. – TheCodingGent Feb 06 '14 at 20:45
  • There's way more to clicking a window than just posting some messages; there's activation and much more. `SendInput` does all this (by generating mouse input); your method does not. – Eric Brown Feb 06 '14 at 22:15
  • @EricBrown Do you think you can provide me an example with SendInput? – TheCodingGent Feb 06 '14 at 22:30
  • @yasman - iljau has an answer & link above that should suffice. My point is that clicking on the screen at an absolute position is your best possible answer; posting messages (especially across processes) is almost certainly guaranteed to fail in an unpredictable manner (as you've already found out). – Eric Brown Feb 06 '14 at 22:34
  • @EricBrown I agree with you for the general case, but it seems that posting mouse messages to the desktop SysListView32 window indeed works. – manuell Feb 07 '14 at 09:46
  • Related: http://stackoverflow.com/questions/2964051/python-win32-simulate-click – Shog9 Feb 08 '14 at 01:38

2 Answers2

3

The following code works with Python33 on Windows 7.

I used ctypes.

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

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

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.

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.

manuell
  • 7,528
  • 5
  • 31
  • 58
  • Why are you using three different handles to get to the desktop, I don't get why simply calling win32gui.GetDesktopWindow() won't return the handle we need? What's progman, then folder then list view doing? – TheCodingGent Feb 06 '14 at 18:58
  • @yasman Did you try my code? It works. If you want to click on an icon, you need to post the message to the SysListView window, not to the Desktop Window. You could use GetDesktopWindow, but then you will end up using the FindWindow APIs anyway to get the SysListView handle. – manuell Feb 07 '14 at 09:41
  • The one thing I still don't get is your parameter 110 + (65536*32) how does that work? – TheCodingGent Feb 07 '14 at 21:53
  • It works man, thank you! I was thinking that win32gui.GetDesktopWindow() actually retrieves the window to the "desktop" that I'm expecting but apparently there's a whole hierarchy of windows behind the desktop I'm looking for and what I want is under the class name SysListView32 just as you mentioned. However your code doesn't work on my machine for me the window Progman has no children, the way I find the list view is by iterating through the children of the desktop window. I'm still having trouble with the x and y parameters though.. – TheCodingGent Feb 07 '14 at 21:58
  • x << 16 is the same thing as x*65536, as 2^16 is 65536. – manuell Feb 08 '14 at 13:00
  • Do you have a theme with picture rotation in place? It seems that the parent of the SHELLDLL_DefView windows, may or may NOT be the progman one, depending on GUI style/configuration. You may workaround that by using the API EnumChildWindows, but it's not so simple. Let me know if your python code is just for personal use, or if you want it to work in all cases. – manuell Feb 08 '14 at 13:07
  • well I actually need it to work in all cases, currently I used spyy++ to find SysListView32 through the hierarchy and I'm able to click the desktop, I even resolved the issue with the coordinates, now the only problem left is that the click is only performed when the window is active and win32gui.SetActive(hwnd) isn't solving the issue – TheCodingGent Feb 10 '14 at 14:12
  • @yasman What window must be active? The window hosting your python code? – manuell Feb 10 '14 at 15:09
  • Nope the window receiving the message, I solved the issue by sending the WM_ACTIVATE message to the desktop (window). – TheCodingGent Feb 10 '14 at 16:23
3

It may be easier to install pywinauto and use ClickInput in combination with find_windows and Rectangle

Links to implementation:

iljau
  • 2,151
  • 3
  • 22
  • 45
  • I've looked at pywinauto it's a very interesting tool however, they still perform a click at an absolute position with respect to the screen and doesn't send a click message to a specific window. – TheCodingGent Feb 06 '14 at 20:48
  • @yasman updated answer a bit. Now you'll have to wait until somebody cherry-picks the necessary parts and writes code snippet that does exactly what you need. – iljau Feb 06 '14 at 22:16
  • Thanks man I'll give it a shot myself and see what I can get. – TheCodingGent Feb 06 '14 at 22:31