python - 为什么子进程启动 "extra"应用程序?

标签 python python-2.7 subprocess

我编写了一个 Python 脚本来检测Print Screen 按键,并启动截图工具。该脚本使用 subprocess.call 来处理应用程序启动。

我面临的问题是,当我使用截图工具并关闭应用程序时,我会启动一个“额外”应用程序。例如,如果我截取了 PowerPoint 演示文稿,那么当我关闭截取工具时,我会自动启动一个新的/空白的 Power Point 实例。我不希望这种情况发生,只想关闭截图工具而不进行任何其他操作。

有人可以帮忙解释一下我的代码中缺少什么吗?

# 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)

最佳答案

我猜测(而且我不在 Windows 上),但我认为 subprocess.call 会等到您完成截图工具,因此 keyboard.press_and_release ('ctrl+N') 将转到 PowerPoint。

关于python - 为什么子进程启动 "extra"应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53854317/

相关文章:

python - 将多个参数从 python 传递到 R

python - 当逐渐给出每条线的点时,在matplotlib中绘制多条线

python - 基于文本长度的django过滤器

python - Matplotlib savefig 背景总是透明的

python - 使用 Python 以点表示法打印唯一的 JSON 键

python - 向抽象语法树中插入一个节点

python - opencv python中的椭圆检测

python - Matplotlib ion() 和子进程

python - 编辑 : Subprocess wont open python file in new window?

python selenium,检索cookie的值并插入到GET中