Hey guys, is it possible to simulate a mouse click using x and y co-ordinates. And also could you have user input of a certain co-ordinate and it would click that specified co-ordinate? thanks.

I found a very neat sample of doing this:

from ctypes import *
import time, curses, win32con, win32gui

PUL = POINTER(c_ulong)

class KeyBdInput(Structure):
    _fields_ = [("wVk", c_ushort),
    ("wScan", c_ushort),
    ("dwFlags", c_ulong),
    ("time", c_ulong),
    ("dwExtraInfo", PUL)]

class HardwareInput(Structure):
    _fields_ = [("uMsg", c_ulong),
    ("wParamL", c_short),
    ("wParamH", c_ushort)]

class MouseInput(Structure):
    _fields_ = [("dx", c_long),
    ("dy", c_long),
    ("mouseData", c_ulong),
    ("dwFlags", c_ulong),
    ("dwExtraInfo", PUL)]

class Input_I(Union):
    _fields_ = [("ki", KeyBdInput),
    ("mi", MouseInput),
    ("hi", HardwareInput)]

class Input(Structure):
    _fields_ = [("type", c_ulong),
    ("ii", Input_I)]

class POINT(Structure):
    _fields_ = [("x", c_ulong),
    ("y", c_ulong)]

def click(x,y):

    orig = POINT()



    FInputs = Input * 2
    extra = c_ulong(0)

    ii_ = Input_I()
    ii_.mi = MouseInput( 0, 0, 0, 2, 0, pointer(extra) )

    ii2_ = Input_I()
    ii2_.mi = MouseInput( 0, 0, 0, 4, 0, pointer(extra) )

    x = FInputs( ( 0, ii_ ), ( 0, ii2_ ) )

    windll.user32.SendInput(2, pointer(x), sizeof(x[0]))

    return orig.x, orig.y

click(150, 150)

It may be long, but it is one of the few examples I have seen that work with little hassle in Python.

For your second question, I'd probably bind with gui. Follow the example here but it doesn't have to be right click:


also, how would you use Tkinter with two buttons for example to save a .txt document all you have to do is click that button and it will make a prompt for the filename and save it. Thanks

This article has been dead for over six months. Start a new discussion instead.