P
P
PosikGG2021-07-27 18:51:04
Python
PosikGG, 2021-07-27 18:51:04

How to make it so that when you hold down the mouse button, the mouse button clicks, and if you release the button, it stops clicking?

Wrote a small code that was supposed to click itself. How to make it so that when the user presses and does not release the mouse button, the program itself clicks, and if the user releases it, then it stops clicking?

import mouse
import keyboard

def click():
    mouse.wait(button='left', target_types=('down'))
    
    if mouse.is_pressed(button='left'):
        while mouse.is_pressed(button='left'):
            mouse.on_click(mouse.click(button="left"), args=())
    click()

click()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
veryoriginalnickname, 2021-07-28
@veryoriginalnickname

As I understand it, one of the main problems in the clique. Mouse.click() knocks down the pressed key. I found a piece of code on Stackoverflow that intercepts all mouse events. I threw in the code, and it works somehow, and it also depends quite heavily on the delay (SLEEP_1 and SLEEP_2). In general, here are my achievements:

import mouse
import time


class Clicker:
    events = []
    mouse.hook(events.append)
    SLEEP_1 = 0.15
    SLEEP_2 = 0.15
    is_released = False
    is_mouse_down = False

    def on_mouse_down(self):
        mouse.click()
        time.sleep(self.SLEEP_1)
        mouse.press()
        print('on_mouse_down')

    def click(self):
        while True:
            mouse._listener.queue.join()
            for event in self.events:
                if hasattr(event, 'event_type'):
                    self.is_mouse_down = event.event_type == 'down'
                    print(self.is_mouse_down)
                    if self.is_mouse_down:
                        print('MOUSE DOWN (ACTIVE)')
                    else:
                        if not self.is_released:
                            print('MOUSE UP (DEACTIVATED)')
                            mouse.release()
                            self.is_released = True
            if self.is_mouse_down:
                self.on_mouse_down()
            del self.events[:]
            time.sleep(self.SLEEP_2)


clicker = Clicker()
clicker.click()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question