Search code examples
pythonpyautogui

How do I make an autoclicker that goes up and down?


I am trying to make an autoclicker that clicks the up key for 10 seconds, then the down key for 10 seconds. I am using the pyautogui module for this and I am getting this error for some reason whenever I run:

keyUp() missing 1 required positional argument: 'key'

This is the rest of the code:

import pyautogui, time
time.sleep(2)
x = 0
times = 20 

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

    else:
        pyautogui.keyUp(), time.sleep(10), pyautogui.keyDown()
        x += 1

Solution

  • Check the pyautogui docs: https://pyautogui.readthedocs.io/en/latest/keyboard.html#the-press-keydown-and-keyup-functions

    The keyUp and keyDown don't correspond to the Up key and the Down key, they correspond to a given key (which you have to supply as the argument) going up and down. For example, keyDown('space') holds the spacebar down and leaves it down until keyUp('space') is called.

    What I think you want is to call the press function on the up and down keys, something like:

    import pyautogui, time
    
    time.sleep(2)
    
    for _ in range(20):
        pyautogui.press("up")
        time.sleep(10)
        pyautogui.press("down")
    
    print("Stopped Clicking")