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, "Pixel Starships")
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, "Pixel Starships")
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, "Pixel Starships")
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?