[Python] How do I hold down a key in python?

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have looked at multiple libraries such as 'pynput', 'pyautogui' and 'keyboard', and can't find a library that holds down a key for a certain amount of time or until a key is pressed. How would I do this. I would like it to be something more like "wwwwwww" - and so w is held down for x amount of time rather than it being "w"+"w"+"w"+"w"+"w"+"w"+"w"

    I tried multiple things such as:

    import pyautogui
    import keyboard as potato
    import time

    potato.wait('del')

    pyautogui.keyDown('shift')
    pyautogui
    time.sleep(5)

    pyautogui.press('shift', 'shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift',)
    pyautogui.keyUp('w')

    pyautogui.hold('w')
    time.sleep(5)
    pyautogui.press('Ctrl')
    pyautogui.keyUp('w')

    Continue reading...

Compartilhe esta Página