Search code examples
pythonpyautogui

How can I hold down the keyboard key with python


Im using the PyAutoGui module, but I cant do a keyboard key hold. Who can say me what im doing wrong or recommend me another python module for keyboard control.

I tryed

with pyautogui.keyDown(key):
   sleep(1)
pyautogui.hold(key, time)
pyautogui.keyDown(key)
sleep(1)
pyautogui.keyUp(key)

etc. And keyboard module hasnt a key hold function too.


Solution

  • pyautogui.keyDown('winleft')
    pyautogui.press('r')
    pyautogui.keyUp('winleft')
    

    try running this code, confirmed 100% working, should bring up the ''run'' window

    Taken from my own script, you can always use the

    time.sleep(1)
    

    function to make it hold for however long you need, just remember to

    import time
    

    at the start of your code (at least that's how i made it work)