I have a script where I have to press different keys on the keyboard using PyAutoGui.
pyautogui.press(['alt','y','2','v','tab','enter'], interval=1)
I need the code to press every key with 1 second of delay between each press, but for some reason the interval value isn't being recognized and press them all at once (I believe there's a 0.1 seconds of delay by default)
I've done it doing this:
pyautogui.press('alt')
time.sleep(1)
pyautogui.press('y')
time.sleep(1)
pyautogui.press('2')
time.sleep(1)
pyautogui.press('v')
time.sleep(1)
pyautogui.press('tab')
time.sleep(1)
pyautogui.press('enter')
But it takes a lot of lines and looks horrible (in my opinion)
The interval
argument in pyautogui.press
does not do what you think it does. If we inspect the function definition...
...
for i in range(presses):
for k in keys:
failSafeCheck()
platformModule._keyDown(k)
platformModule._keyUp(k)
time.sleep(interval)
This shows us that the interval is actually a pause between the entire key sequence. For example, let us say you want to press a
, b
, c
twice, with 1 second between each abc
. You can use pyautogui.press(['a', 'b', 'c'], presses=2, interval=1)
, and you'll get
abc # no pause between a, b, c
# 1 second pause
abc # no pause between a, b, c
To enforce an interval between each key in the list, try using pyautogui.hotkey
as such:
pyautogui.hotkey('a', 'b', 'c', interval=0.5)
The caveat here being that the keyUp also has an interval, so you get a significant pause at the end of the function as well.