-1

I'm pretty new to Python and I'd like to make a kind of an Autoclicker, which keeps clicking every 0.1 seconds when my left mouse button is held down. My Problem is, that when I run my script, my mouse instantly starts clicking. What should I do?:

import win32api
import time
from pynput.mouse import Button, Controller
mouse = Controller()

while True:

    if win32api.GetAsyncKeyState(0x01):
        mouse.click(Button.left, 1)
        time.sleep(0.1)
    else:
        pass

Thanks

Dom
  • 47
  • 2
  • 7

3 Answers3

0

Check if win32api.GetAsyncKeyState(0x01) < 0 in the if condition.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Now, the only thing it does is click once, which makes my click a double click. What should I do to make it keep clicking when I hold down my mouse button? It's even the same when I turn the 'if' into a 'while. – Dom May 21 '20 at 21:17
0

My Problem is, that when I run my script, my mouse instantly starts clicking. What should I do?

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.

You should use win32api.GetAsyncKeyState(0x01)&0x8000 instead.

Now, the only thing it does is click once, which makes my click a double click.

Because GetAsyncKeyState detects the key state of the left mouse button. When you press the left mouse button, the click function is called, click will realize the two actions of the left mouse button press and release. Then in the place of the while loop, GetAsyncKeyState will detect the release action, which is why it stops after double-clicking.

I suggest you set the left mouse button to start and the right mouse button to stop.

Code Sample:

import win32api
import time
from pynput.mouse import Button, Controller
mouse = Controller()

while True:

    if (win32api.GetAsyncKeyState(0x01)&0x8000 > 0):
        flag = True
        while flag == True:
               mouse.click(Button.left, 1)
               time.sleep(0.1)
               if (win32api.GetAsyncKeyState(0x02)&0x8000 > 0):
                   flag = False
    else:
        pass
Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • Yeah that thing wouldn't be a problem, but I want it to actually work while i hold down my left mouse button. Is there any possible way to do that? – Dom May 22 '20 at 10:08
  • @user13591244 The left mouse button has two states: pressed and released. Because the mouse is continuously clicking, using `GetAsyncKeyState` cannot accurately capture the correct release state which you want to stop clicking. I suggest you use the left and right buttons to control separately. – Strive Sun May 22 '20 at 10:34
  • Alright, so it's not possible. I'll try to make it that I have to hold down mouse5. Thanks anyways. – Dom May 22 '20 at 10:38
0

I made it work with mouse5!

import win32api
import win32con
import time
from pynput.mouse import Button, Controller
mouse = Controller()

def ac():
    if keystate < 0:
        mouse.click(Button.left, 1)
        time.sleep(0.1)
    else:
        pass

while True:
    keystate = win32api.GetAsyncKeyState(0x06)
    ac()
Dom
  • 47
  • 2
  • 7