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.

Dani AI

Generated

Nice question, . If you only need to click a specific screen coordinate, ’s SendInput approach is perfect for Windows. If you want a simpler, cross‑platform way (Windows/macOS/Linux) that accepts user‑entered X/Y, try PyAutoGUI. It treats (0,0) as the top-left of the primary display and includes a fail‑safe (slam the cursor into a corner to abort). Example:

import time, pyautogui

x = int(input('x: '))
y = int(input('y: '))

time.sleep(2)  # switch focus to the target app
pyautogui.moveTo(x, y)
pyautogui.click()

Tip: test positions with pyautogui.position() in a REPL to confirm DPI/scale is not offsetting your clicks. (pyautogui.readthedocs.org)

On Windows specifically, remember injected input cannot target a higher‑integrity window (for example, an elevated/UAC prompt) from a normal process. If clicks seem to do nothing on those dialogs, run your script with the same or higher integrity (Run as administrator) or avoid automating those prompts. That limitation comes from UIPI and applies to SendInput. (learn.microsoft.com)

For your Tkinter follow‑up, you can wire two buttons so one does a quick save and the other prompts for a filename using tkinter.filedialog:

import tkinter as tk
from tkinter import filedialog

last_path = None

def save_as():
    global last_path
    path = filedialog.asksaveasfilename(defaultextension='.txt',
                                        filetypes=[('Text', '*.txt')])
    if path:
        with open(path, 'w', encoding='utf-8') as f:
            f.write(txt.get('1.0', 'end-1c'))
        last_path = path

def save():
    if last_path:
        with open(last_path, 'w', encoding='utf-8') as f:
            f.write(txt.get('1.0', 'end-1c'))
    else:
        save_as()

root = tk.Tk()
txt = tk.Text(root, width=40, height=8); txt.pack()
tk.Button(root, text='Save', command=save).pack(side='left')
tk.Button(root, text='Save As...', command=save_as).pack(side='left')
root.mainloop()

asksaveasfilename is the simple, portable way to show a native Save dialog. (docs.python.org)

Recommended Answers

All 4 Replies

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),
    ("time",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()

    windll.user32.GetCursorPos(byref(orig))

    windll.user32.SetCursorPos(x,y)

    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:

http://www.daniweb.com/forums/post598858.html

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

If you're talking about mouse clicks in Tkinter, then you will want to check out the manual:

OR in HTML format


To get mouse clicks, check out the Bind method at the end of the manual. To handle file dialogs, check out the tkFileDialog module.

Hope that helps,
Jeff

thanks answered my question

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.