background clicks

Posts 1–4 of 4 · Page 1 of 1
background clicks
I had a need to click on the coordinates once every 30 seconds when the game is inactive in full screen window mode, I spent a lot of time but it is apparently impossible to do on ahk, and I couldn't figure it out on C# C++, maybe there is someone here who can make such a program? X 333 Y 242
Quote Originally Posted by 3rd View Post
I had a need to click on the coordinates once every 30 seconds when the game is inactive in full screen window mode, I spent a lot of time but it is apparently impossible to do on ahk, and I couldn't figure it out on C# C++, maybe there is someone here who can make such a program? X 333 Y 242
I see a lot of people trying to send clicks to a game, and honestly, it's almost impossible using ControlClick in AHK.

That's because the game runs with DirectX or OpenGL, and those kinds of UIs are tricky to deal with.

I have a simple code here that gets around that — it sends mouse clicks (left or right) directly to the game. You can even send other mouse buttons too.

This is a simple script I made that you can try out.

All you need to do is install any version of Python (3.x or above).
Just make sure you enable the "Add python.exe to PATH" option during installation.

Once it's installed, open CMD and run this command to install the required library:

Code:
pip install pywin32
Now you can run the code, and it should work just fine.

python code:

Code:
import time, win32gui, win32con

def send_mouse_click(windowID, button="left", x=0, y=0, state=None):
    """
    Sends a mouse click (left or right) to a specific window using SendMessage.

    A fixed coordinate (x, y) is used for the click location; you can modify this if needed.

    Parameters:
        windowID: Handle of the target window.
        button (str): Type of click ("left" or "right").
        state (str or None): State of the click:
            - None: Sends a full click (down and up).
            - "down": Sends only the mouse button down event.
            - "up": Sends only the mouse button up event.
    """
    lParam = (y << 16) | (x & 0xFFFF)  # Combine x and y into a single lParam value

    button = button.lower()

    if button == "left":
        if state is None:
            # Send left mouse button down and up
            win32gui.PostMessage(windowID, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
            win32gui.PostMessage(windowID, win32con.WM_LBUTTONUP, 0, lParam)
        elif state.lower() == "down":
            # Send only left mouse button down
            win32gui.PostMessage(windowID, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
        elif state.lower() == "up":
            # Send only left mouse button up
            win32gui.PostMessage(windowID, win32con.WM_LBUTTONUP, 0, lParam)
        else:
            raise ValueError("State for mouse click must be 'down', 'up' or None.")
    elif button == "right":
        if state is None:
            # Send right mouse button down and up
            win32gui.PostMessage(windowID, win32con.WM_RBUTTONDOWN, win32con.MK_RBUTTON, lParam)
            win32gui.PostMessage(windowID, win32con.WM_RBUTTONUP, 0, lParam)
        elif state.lower() == "down":
            # Send only right mouse button down
            win32gui.PostMessage(windowID, win32con.WM_RBUTTONDOWN, win32con.MK_RBUTTON, lParam)
        elif state.lower() == "up":
            # Send only right mouse button up
            win32gui.PostMessage(windowID, win32con.WM_RBUTTONUP, 0, lParam)
        else:
            raise ValueError("State for mouse click must be 'down', 'up' or None.")
    else:
        raise ValueError("Button must be 'left' or 'right'.")

# Example usage: repeatedly send left-click to a window named "Trove"
hwnd = win32gui.FindWindow(None, "Trove")
while True:
    send_mouse_click(hwnd, "left", 333, 242)
    time.sleep(30)  # Wait 30 seconds between clicks
I have already installed Python and when I tried to update it, nothing worked for me, perhaps because of the old version I have a problem with executing the script code, the problem is that after launching the script the console window simply closes, can you advise me how to solve this problem?

I downloaded the latest version of python from the site but the command line shows the old one
https://live.staticflickr.com/65535/...6af48db5_o.png

- - - Updated - - -

I fixed the error, but the window still closes

- - - Updated - - -

I reinstalled python, the window does not close now but nothing happens after running the script
Quote Originally Posted by 3rd View Post
I have already installed Python and when I tried to update it, nothing worked for me, perhaps because of the old version I have a problem with executing the script code, the problem is that after launching the script the console window simply closes, can you advise me how to solve this problem?

I downloaded the latest version of python from the site but the command line shows the old one
https://live.staticflickr.com/65535/...6af48db5_o.png

- - - Updated - - -

I fixed the error, but the window still closes

- - - Updated - - -

I reinstalled python, the window does not close now but nothing happens after running the script

The Python version I have is 3.11.9 (64-bit).

Also, make sure to install the required libraries using the following command:
Code:
pip install pywin32
I added debug logging to the script, so if any error occurs, it will be clearly printed for you.

Please note: The clicking will only work when the game window is in the background.
This means if the script is running and you are actively inside the game (foreground), you will not see any clicks. That is normal it only works when the window is in the background.

Code:
import time
import win32gui, win32con
import logging

# ——— إعداد اللوجينغ ———
logging.basicConfig(
    level=logging.INFO,
    format='[%(asctime)s] %(levelname)s: %(message)s',
    datefmt='%H:%M:%S'
)

def send_mouse_click(windowID, button="left", x=0, y=0, state=None):
    """
    Sends a mouse click (left or right) to a specific window using PostMessage.
    """
    lParam = (y << 16) | (x & 0xFFFF)
    button = button.lower()

    try:
        if button == "left":
            if state is None:
                win32gui.PostMessage(windowID, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
                win32gui.PostMessage(windowID, win32con.WM_LBUTTONUP, 0, lParam)
            elif state == "down":
                win32gui.PostMessage(windowID, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
            elif state == "up":
                win32gui.PostMessage(windowID, win32con.WM_LBUTTONUP, 0, lParam)
            else:
                raise ValueError("State must be 'down', 'up' or None.")
        elif button == "right":
            if state is None:
                win32gui.PostMessage(windowID, win32con.WM_RBUTTONDOWN, win32con.MK_RBUTTON, lParam)
                win32gui.PostMessage(windowID, win32con.WM_RBUTTONUP, 0, lParam)
            elif state == "down":
                win32gui.PostMessage(windowID, win32con.WM_RBUTTONDOWN, win32con.MK_RBUTTON, lParam)
            elif state == "up":
                win32gui.PostMessage(windowID, win32con.WM_RBUTTONUP, 0, lParam)
            else:
                raise ValueError("State must be 'down', 'up' or None.")
        else:
            raise ValueError("Button must be 'left' or 'right'.")
    except Exception as e:
        logging.error(f"send_mouse_click error: {e}")
        raise  # إعادة رفع الاستثناء إذا بغيت يوقف الكود
    else:
        logging.debug(f"Clicked {button} at ({x},{y}), state={state}")

if __name__ == "__main__":
    hwnd = win32gui.FindWindow(None, "Trove")
    if not hwnd:
        logging.critical("Window 'Trove' not found! Make sure the game is running.")
    logging.info(f"Found window handle: {hwnd}")

    try:
        while True:
            send_mouse_click(hwnd, "left", 333, 242, state='down')
            logging.info("Sent left-click to Trove at (333,242)")
            time.sleep(30)
    except KeyboardInterrupt:
        logging.info("Interrupted by user, exiting.")
    except Exception as e:
        logging.exception(f"Unexpected error in main loop: {e}")
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?