I needed something similar, and the solution I found was this:
import win32guiimport win32apiimport win32condef control_click(x, y, handle, button='left'): l_param = win32api.MAKELONG(x, y) if button == 'left': win32gui.PostMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, l_param) win32gui.PostMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, l_param) elif button == 'right': win32gui.PostMessage(handle, win32con.WM_RBUTTONDOWN, 0, l_param) win32gui.PostMessage(handle, win32con.WM_RBUTTONUP, 0, l_param)
The window does not need to be in the foreground, but it cannot be minimized, the handle and coordinates must be from the control, example: in a notepad, the handle and coordinates are for the 'Edit1' control, not for the window, if the application has no controls visible through a spytool, you can use the handle and the window coordinates, example: the pycharm itself.