Search code examples
pythonwindowsclickmouseeventmouse

How to make python script that performs a Mouse Click in Windows 10?


I'm trying a simple python script, it clicks on a screen coordinate.

I've tried with Pyautogui, pynput, pydirectinput, pywinauto... But in none of them the click is actually made, the only thing that works is to move the mouse to the coordinate.

the scripts are simple, but it still doesn't work, by deduction I think it's a win10 related problem.

Does anyone know how I can solve this?

Do I need to install anything else, maybe a driver?

Is it missing to give some kind of permission?

is there a way for me to give command to the mouse hardware to make the click, instead of being a virtualized click?

Some of my attempts below

OBS: In all attempts the mouse moves, but does not click.

Pyautogui:

import pyautogui
pyautogui.moveTo(35, 240)
pyautogui.click() 

Pydirectinput:

import pyautogui
import pydirectinput
pydirectinput.moveTo(35, 240)
pydirectinput.click()

pywinauto:

import pywinauto
from pywinauto import Desktop, Application, mouse, findwindows

pywinauto.mouse.move(coords=(160, 400))
pywinauto.mouse.double_click(button='left', coords=(160, 400))

Direct windows click:

import win32api, win32con
def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,200)

Autoclicker using pynput:

import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode


delay = 0.001
button = Button.left
start_stop_key = KeyCode(char='s')
exit_key = KeyCode(char='e')


class ClickMouse(threading.Thread):
    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True

    def start_clicking(self):
        self.running = True

    def stop_clicking(self):
        self.running = False

    def exit(self):
        self.stop_clicking()
        self.program_running = False

    def run(self):
        while self.program_running:
            while self.running:
                mouse.click(self.button)
                time.sleep(self.delay)
            time.sleep(0.1)


mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()


def on_press(key):
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
    elif key == exit_key:
        click_thread.exit()
        listener.stop()


with Listener(on_press=on_press) as listener:
    listener.join()

Solution

  • Try this way:

    import pywinauto
    app = pywinauto.Application().connect(path='your_process_name.exe')
    app.MainDialog.click_input(coords=(x, y))
    

    For click method to work you need to specify the process/dialog on which the coordinate is present. Use connect() method to connect to a existing method else use start() to open new instance.