Below is a minimal but complete example that does exactly what you asked for:
Waits until you LEFT-click once inside the game window and remembers that spot.
Converts the spot to “relative” coordinates (0 – 1 range).
Lets you call right_click_relative() any time later; it will send a RIGHT-click directly to the window (no visible mouse movement) – even if the window was resized, minimised, moved, etc.
The code uses nothing except the standard library + pywin32 (install with pip install pywin32).
python
# cabal_clicker.py
import time
import win32api
import win32con
import win32gui
###############################################################################
# Helper functions
###############################################################################
def get_hwnd(window_title_part: str) -> int:
"""
Returns the window handle (HWND) of the first top-level window whose title
contains 'window_title_part'. Raises ValueError if not found.
"""
def enum_handler(hwnd, result_list):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
if window_title_part.lower() in title.lower():
result_list.append(hwnd)
found = []
win32gui.EnumWindows(enum_handler, found)
if not found:
raise ValueError(f"No window containing '{window_title_part}' found.")
return found[0]
def get_client_size(hwnd: int) -> tuple[int, int]:
"""Returns (width, height) in *client* coordinates (no borders, no titlebar)."""
left, top, right, bottom = win32gui.GetClientRect(hwnd)
return right - left, bottom - top
def screen_to_client(hwnd: int, pt: tuple[int, int]) -> tuple[int, int]:
"""Converts a screen point to client coordinates for hwnd."""
return win32gui.ScreenToClient(hwnd, pt)
###############################################################################
# 1) --- RECORDING A RELATIVE POSITION ---------------------------------------
###############################################################################
def record_relative_point(hwnd: int, verbose=True) -> tuple[float, float]:
"""
Blocks until the user does a *left* click **inside** the given window.
Returns the click position as floats in range 0–1 (x_rel, y_rel).
"""
if verbose:
print("Waiting for LEFT-click inside the window to record position…")
while True:
# VK_LBUTTON state bit 15 == key pressed
if win32api.GetAsyncKeyState(win32con.VK_LBUTTON) & 0x8000:
x_screen, y_screen = win32api.GetCursorPos()
target_hwnd = win32gui.WindowFromPoint((x_screen, y_screen))
# Accept clicks on main window *or* any child control
if target_hwnd == hwnd or win32gui.IsChild(hwnd, target_hwnd):
x_client, y_client = screen_to_client(hwnd, (x_screen, y_screen))
w, h = get_client_size(hwnd)
# Guard against clicks on resize border (zero area)
if w and h:
x_rel = x_client / w
y_rel = y_client / h
if verbose:
print(f"Recorded relative position: ({x_rel:.4f}, {y_rel:.4f})")
# Debounce – wait until button released
while win32api.GetAsyncKeyState(win32con.VK_LBUTTON) & 0x8000:
time.sleep(0.01)
return x_rel, y_rel
time.sleep(0.01) # Be nice to the CPU
###############################################################################
# 2) --- SENDING A RIGHT-CLICK WITHOUT TOUCHING THE REAL MOUSE ---------------
###############################################################################
def right_click_relative(hwnd: int, x_rel: float, y_rel: float):
"""
Sends a RIGHT mouse click directly to 'hwnd' at a *relative* location.
The click is posted with WM_RBUTTONDOWN / WM_RBUTTONUP – your real cursor
never moves.
"""
w, h = get_client_size(hwnd)
x = int(x_rel * w)
y = int(y_rel * h)
lparam = (y << 16) | x # Win32 expects y in high-word, x in low-word
# --- send the two messages ---
win32api.PostMessage(hwnd, win32con.WM_RBUTTONDOWN, win32con.MK_RBUTTON, lparam)
win32api.PostMessage(hwnd, win32con.WM_RBUTTONUP, 0, lparam)
###############################################################################
# Example usage ---------------------------------------------------------------
###############################################################################
if __name__ == "__main__":
TARGET_TITLE = "Cabal" # anything contained in the title bar text
hwnd_game = get_hwnd(TARGET_TITLE)
print(f"Found window 0x{hwnd_game:08X} – '{win32gui.GetWindowText(hwnd_game)}'")
# ------------------------------------------------------------------------
x_rel, y_rel = record_relative_point(hwnd_game) # one-time recording
# ------------------------------------------------------------------------
print("\nPress <Enter> whenever you want to fire a RIGHT-click at that spot.")
print("Ctrl-C to quit.\n")
try:
while True:
input()
right_click_relative(hwnd_game, x_rel, y_rel)
print(" …right-click sent!")
except KeyboardInterrupt:
print("\nBye!")
How it works
We locate the Cabal Online window with EnumWindows and grab its handle (HWND).
record_relative_point() waits for one left-click inside that window, turns the coordinates into normalised values (x_rel, y_rel).
right_click_relative() converts the stored relative values back to current pixel coordinates (using the current client size) and fires the standard WM_RBUTTONDOWN / WM_RBUTTONUP messages – the cursor never moves.
Why it is window-size independent
The recorded (x_rel, y_rel) are saved as fractions (e.g. “0.42 of the width, 0.33 of the height”).
When the window is later resized, we multiply those fractions by the new width / height – voilà, the click lands in the same logical spot.
A few practical notes
• You need to run the script with the same privileges as the game (so: “Run as Administrator” if Cabal is elevated).
• Some anti-cheat engines block or monitor injected messages. Your private server may not care, but be aware before aiming this at retail games.
• DirectInput usually still receives WM_* messages; if Cabal was switching to raw DirectInput and ignores them, you’d have to resort to SendInput with the MOUSEEVENTF_ABSOLUTE flag – but for most DirectX titles the above works fine.