Untitled

 avatar
unknown
plain_text
a month ago
1.5 kB
2
Indexable
import time

# Define necessary structures and constants
PUL = ctypes.POINTER(ctypes.c_ulong)

class MOUSEINPUT(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class INPUT_I(ctypes.Union):
    _fields_ = [("mi", MOUSEINPUT)]

class INPUT(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", INPUT_I)]

INPUT_MOUSE = 0
MOUSEEVENTF_MOVE = 0x0001
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004

def click(x, y):
    # Get current screen resolution
    screen_width = ctypes.windll.user32.GetSystemMetrics(0)
    screen_height = ctypes.windll.user32.GetSystemMetrics(1)

    # Calculate normalized absolute coordinates
    x = int(x * (65536 / screen_width) + 0.5)
    y = int(y * (65536 / screen_height) + 0.5)

    # Create input structures for the mouse click
    extra = ctypes.c_ulong(0)
    ii = INPUT_I()
    ii.mi = MOUSEINPUT(x, y, 0, MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, ctypes.pointer(extra))
    command = INPUT(ctypes.c_ulong(INPUT_MOUSE), ii)

    # Send the input
    ctypes.windll.user32.SendInput(1, ctypes.pointer(command), ctypes.sizeof(command))

# Example usage: Click at the current cursor position
time.sleep(2)  # Wait for 2 seconds before clicking
click(0, 0)  # Click at the top-left corner for demonstration
Leave a Comment