Pyautogui malfunctions?

18 Views Asked by At

I want to hold backspace button for several seconds then release it, but it only clicks 1 once (deletes a single character) Latest version of both modules

import pyautogui
import time
time.sleep(2)
pyautogui.keyDown('backspace')
time.sleep(5)
pyautogui.keyUp('backspace')

this is what I tried expecting it to hold backspace for 5 seconds, but it only deletes a single character

1

There are 1 best solutions below

3
Genchev On BEST ANSWER

The pyautogui.keyDown and pyautogui.keyUp functions are not designed to hold a key down for a duration. They are designed to simulate the press and release of a key respectively.

Easy way to do what you want is to use a loop that presses the backspace key repeatedly for the duration, as the example

import pyautogui
import time

time.sleep(2)
start_time = time.time()  # remember when we started
while (time.time() - start_time) < 5:  # for the next 5 seconds
    pyautogui.press('backspace')  # press the backspace key

In this code, pyautogui.press('backspace') is called repeatedly for 5 seconds. This will have the effect of holding down the backspace key. You can change the seconds as you need/want.