EN VI

Python-3.x - Pyautogui malfunctions?

2024-03-13 14:30:05
How to Python-3.x - Pyautogui malfunctions?

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

Solution:

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.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login