Search code examples
pyautogui

PyAutoGUI error attempting to use pyautogui.press()


Trying to get PyAutoGUI to press both z button and x button at the same time on the keyboard causes an error. It assumes the second entry is presses, but it's not.

I have refered to the official documentation and accordingly this should work, yet fails.


import pyautogui
from time import sleep

x = 0
stop = 10
while x < stop:
    pyautogui.press('z', 'x')
    sleep(0.1)
    x+=1

From Official Documentation:

To press multiple keys similar to what write() does, pass a list of strings to press(). For example:

pyautogui.press(['left', 'left', 'left'])

Or you can set how many presses left:

pyautogui.press('left', presses=3)


Solution

  • My bad, my eyes failed to see the [ ]'s, and now it works. Answered my own question.

    import pyautogui
    from time import sleep
    
    x = 0
    stop = 10
    while x < stop:
        pyautogui.press(['z', 'x'])
        sleep(0.1)
        x+=1