0

so I'm using pyautogui to type, and I'm trying to hold a key down for more than a second but I've ran into this problem where the key only types a single letter

import pyautogui
import time

pyautogui.keyDown("w")
time.sleep(2)
pyautogui.keyUp("w")

my output is "w" but my output should be "wwwwwwwwwwwwwww" since I'm holding down the key?

the same thing occurs when im using the press function for pyautogui,

pyautogui.press("w") #but instead of pressing a single key, it totally just doesnt get outputted but only works for main keyboard functions like windowsKey and enter

if this is wrong, is their a way i can make it so I'm holding down the key?

Shavow
  • 82
  • 9
  • Similar problem in this thread from 2018: https://stackoverflow.com/questions/48682388/pyautogui-press-key-for-x-seconds – Contrean Jul 20 '21 at 10:46

1 Answers1

2

From the Documentation it seems that it's not possible to do the way you have tried, however this function can help for 'holding down' a letter for a set number of seconds:

def hold_character(hold_time, character, interval=0.1):
    pyautogui.write(character * int(hold_time / interval), interval=interval)

hold_character(2, 'w')

...gives the 'wwwwwwwwwwwwwww' effect for me