'How to make an autoclicker in python?

I am trying to make a simple autoclicker, I want it so that if i press a command key, ('a' for example), it will hold down the down arrow for 10 seconds, then the up arrow for 10 seconds. Repeat cycle until 'a' is clicked again. This command should be universal to the rest of the desktop as well.

import time

running = True

if running == True:
    print("a"), time.sleep(3), print("b")

instead of print a/b i want it to press the up key on my keybaord and down key, and running = True should ben changed to a key command.



Solution 1:[1]

First install pyautogui using pip

pip install pyautogui

Code:-

import pyautogui, time
time.sleep(2)
x = 0
times = 20 # Change this to the number of times you want it to click

while True:
    if x == times:
        print("Stopped Clicking")
        break

    else:
        pyautogui.leftClick()
        x += 1

This code left clicks the number of times set in the variable times (Here, it is 20 times)

Solution 2:[2]

Keyboard Packages

You can use this package to do actions on a keyboard keypress.

Before using you should install it:

python3 -m pip install keyboard

And then here's how to use it:

import keyboard as kb
while True:
    if keyboard.is_pressed("space"):
        print("Space pressed.")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Biswajit Paloi
Solution 2 Liam