I'm trying to write a function to allow me to send a left click to x, y
co-ordinates on screen without my mouse cursor moving.
I've read through the send message documentation and the mouse input notification documentation and I've come up with a few different approaches, none of which work. And none throw an error.
I'm using win32gui.FindWindow
to get the hwnd
and then I've tried using both PostMessage
and SendMessage
to perform the click, none so far work.
import win32api, win32gui, win32con
def control_click(x, y):
hWnd = win32gui.FindWindow(None, "xxxx")
l_param = win32api.MAKELONG(x, y)
if button == 'left':
win32gui.PostMessage(hWnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, l_param)
win32gui.PostMessage(hWnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, l_param)
control_click(526, 694)
def click(x, y):
hWnd = win32gui.FindWindow(None, "xxx")
lParam = win32api.MAKELONG(x, y)
win32api.SendMessage(hWnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
win32api.SendMessage(hWnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lParam)
click(526,694)
def leftClick(pos):
hWnd = win32gui.FindWindow(None, "xxx")
lParam = win32api.MAKELONG(pos[0], pos[1])
win32gui.SendMessage(hWnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
win32gui.SendMessage(hWnd, win32con.WM_LBUTTONUP, 0, lParam)
leftClick([526,964])
What do I need to do to get this function to work?
I have been looking for an answer to this question for over 2 years. I have tried all possible scripts, nothing worked (well, pywinauto works fine in a lot of cases, but with some apps, it doesn't). I found something yesterday that I want to share with you. It is not a script for mouse clicks, but for keystrokes. However, it might be the way to solve your (our) problem. I have tested it already with some apps that gave me trouble sending keystrokes, and it worked like a charm. I hope I can get it to run with mouse clicks. Keep us informed if you find something, I will also keep looking for a solution.
https://github.com/byt3bl33d3r/Invoke-AutoIt
Loads the AutoIt DLL and PowerShell assemblies into memory and executes the specified keystrokes.
This was mainly created to bypass some application restrictions when it came to sending keystroke input via sendkeys(), sendinput() or sendmessage(). e.g. Windows's Remote Desktop Client ;)
Edit: I found the solution! It is possible, but you need to know assembly! Have a look at this video that I have just found https://www.youtube.com/watch?v=T5sXoEEPFBQ
For the ones that don't know assembly, and need to use one of the common tools, here is my experience:
Pywinauto: Works best for sending "clicks" to a background window, but many times it doesn't work like you expected. https://pywinauto.readthedocs.io/en/latest/
AHK: The most reliable mouse clicker is, in my opinion, AHK: https://pypi.org/project/ahk/ (This is, for example, the only mouse clicker that I found which works with Roblox). However, it doesn't work well with background windows (at least for me)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With