1

I wrote a Python script to detect a Print Screen keypress, and launch the Snipping Tool. The script uses subprocess.call to handle the application launch.

The problem I am facing is that when I am done with Snipping Tool and close the application, I get an "extra" application being launched. For example, if I took a snip of a PowerPoint presentation, then when I close Snipping Tool I get a new / blank instance of Power Point being launched automatically. I do not want this to happen, and just want the Snipping Tool to close without any additional action.

Can someone please help explain what I am missing in my code?

# https://stackoverflow.com/questions/24072790/detect-key-press-in-python
# https://pypi.org/project/keyboard/
# https://github.com/boppreh/keyboard#api

import keyboard #pip install keyboard
import time
import subprocess

while True:
    if keyboard.is_pressed('print screen'):
        subprocess.call(r'SnippingTool.exe') # blocking; waits until open
        keyboard.press_and_release('ctrl+N')
    #elif keyboard.is_pressed('ctrl+print screen'): # not recognizing "print screen" here
    elif keyboard.is_pressed('ctrl+esc'):        
        print 'killing it now'
        break
    else:
        time.sleep(0.1)
Roberto
  • 2,054
  • 4
  • 31
  • 46

1 Answers1

2

I'm guessing (and I'm not on Windows), but I think that the subprocess.call waits until you have finished with Snipping Tool, and so the keyboard.press_and_release('ctrl+N') is going to PowerPoint.

Win
  • 551
  • 2
  • 5