0

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)
RCarmody
  • 712
  • 1
  • 12
  • 29
  • 1
    2 things. (1) Is the player window active? If not, you'll have to activate it somehow for keypresses to work. If you're on Windows, there is an undocumented command `getWindowsWithTitle` that you can use to activate() your player. (2) Does only the left shift key work to jump forward? In any case I suspect that @winston1420 answer might be a little better than your approach. If you truly need only the left shift key, you can modify @winston1420 answer to `pe.hotkey('shiftleft', 'left')` – bfris Oct 25 '21 at 20:50
  • it seems to be a shift issue, not a video, window, or 'right' issue... any ideas why? – RCarmody Oct 26 '21 at 17:17

3 Answers3

0

I think you should use the pyautogui.hotkey() function instead.

Like this:

import pyautogui as pe

pe.hotkey('shift', 'left)

It basically does the same thing, it presses shift and left and does the thing.

winston1420
  • 160
  • 9
  • 1
    This still does not work - even when I leave it on a normal paragraph of text, shiftleft/right should start to highlight the string to the right of the cursor, but all it's doing is moving the cursor to the right (both normal and with hotkey) – RCarmody Oct 26 '21 at 17:16
0

Here's another answer that doesn't require installing pydirectinput. This answer applies to Windows only. From the comments in this question, having numlock or caps lock activated will break <shift> + <arrow> in pyautogui. To get around this, you'll have to detect if either numlock or capslock is active. To do that, you'll have to do Windows API calls. You can do the calls with built-in ctypes or pywin32

Here's an example for ctypes (ctypes code adapted from this answer):

import pyautogui
import time

mywin = pyautogui.getWindowsWithTitle("Notepad")[0]

mywin.activate()
time.sleep(2)

import ctypes
VK_CAPITAL = 0x14
VK_NUMLOCK = 0x90
user32 = ctypes.WinDLL('user32.dll')

if user32.GetKeyState(VK_CAPITAL):
    # numlock is active, need to deactivate it before using <shift>+<arrow>
    capslock = True
    pyautogui.press('capslock')
else:
    capslock = False

if user32.GetKeyState(VK_NUMLOCK):
    # capslock is active, need to deactivate it before using <shift>+<arrow>
    numlock = True
    pyautogui.press('numlock')
else:
    numlock = False

pyautogui.keyDown('shift')
pyautogui.press('right')
pyautogui.keyUp('shift')

if capslock:
    pyautogui.press('capslock')

if numlock:
    pyautogui.press('numlock')
bfris
  • 5,272
  • 1
  • 20
  • 37
-1

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')
bfris
  • 5,272
  • 1
  • 20
  • 37