How to make a Python script run a Click on Windows 10?

Asked

Viewed 205 times

0

I’m trying a simple script with python, it click on a screen coordinate.

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

the scripts are simple, but still does not work, by deduction I think it is a problem related to win10.

Does anyone know how I can fix this?

I need to install something else, maybe a driver?

We’re missing some kind of permission?

there is some way I can give command to the mouse hardware to click instead of being a virtualized click?

I’m lost :(

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()
  • You tried to pyautogui.click(x=35, y=240)?

  • yes, it moves no more click

  • if the user has quiet administrative permission, ja the api it raises an Exception alert indicating that it needs authorization, but it seems that the api has already dealt with this issue according to this question asked "https://stackoverflow.com/questions/38486151/pyautogui-click-permissions-error" . The pyautogui lib makes it possible to choose which mouse button will be used tbm.

  • In this case, use it the way you did, but put a time.sleep(1) between move and click

  • I’m running powershell as an administrator, and it still doesn’t work: https://i.imgur.com/Fwhfcsf.gif Paulo Marques, it doesn’t even work with teams. I tried already

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.