3

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()
Ridjin
  • 33
  • 1
  • 4
  • Does the click only not work in certain software, like a game? If so it could be that the game uses raw mouse hardware input, and so would bypass a programmatic click done via this method. Or, is it not working anywhere? – Random Davis Feb 19 '21 at 18:07
  • Yes, my focus is to click on a game. But he doesn’t click on anything, not even in tests on vsCode I managed to make him click! There is software like Blue Eye Macro that clicks on any application. But I can't depend on it. Is there any way I can give a command to click the mouse hardware? – Ridjin Feb 19 '21 at 18:24
  • What person do you mean by "him"? I'm not sure what you're saying. Are you saying that this script doesn't cause clicks to be registered in VSCode? But the application "Blue Eye Macro" does work, for both VSCode and the game? – Random Davis Feb 19 '21 at 18:36
  • Sorry, "him" is the script haha. I tried click in everything. vsCode, Google Chrome, notepad, but doenst work in anything. – Ridjin Feb 19 '21 at 18:46
  • Maybe give ctypes a try: https://stackoverflow.com/a/1181539/1698225 – Alex Jadczak Feb 19 '21 at 21:08

1 Answers1

3

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.

Vidya Marathe
  • 403
  • 2
  • 6
  • 17