Search code examples
pythonpyautogui

PyAutoGUI Shift-Down does Nothing


I have the following script that is intended to take a screenshot of a video every 5sec by taking a screenshot, then hitting shift-right to fast-foward to the next 5sec interval, repeat. It looks like the shiftdown is not working, as whenever i do it manually, it works but whenever I run the script, the right button works but no shift.

time.sleep(2)

while t < 20:
    time.sleep(0.5)
    pyautogui.keyDown('shiftleft')
    time.sleep(0.5)
    pyautogui.press('right')
    pyautogui.keyUp('shiftleft')
    time.sleep(2)
    screenshot = pyautogui.screenshot()
    screenshot.save(loc + str(t) + '.png')
    t = t + 1

time.sleep(0.5)

Solution

  • If you are using Windows AND if you are willing to try a different module, pydirectinput seems to work better than pyautogui for <shift>+<arrow> key operations. I got the idea from this answer

    The example below uses an open copy of Notepad. In Notepad, <shift> + <arrow> will select text. For this example to work, you'll want a some spaces in your file and the cursor would have to be to the left of at least one space.

    import pyautogui
    import pydirectinput
    import time
    
    mywin = pyautogui.getWindowsWithTitle("Notepad")[0]
    
    mywin.activate()
    time.sleep(2)
    
    pydirectinput.keyDown('shift')
    pydirectinput.press('right')
    pydirectinput.keyUp('shift')