【问题标题】:Why my app keep opening when I use shortcut in Python?当我在 Python 中使用快捷方式时,为什么我的应用程序一直打开?
【发布时间】:2020-12-26 16:36:24
【问题描述】:

我尝试创建一个程序以使用特定快捷方式打开应用程序,但是当我按下我的键时它一直打开并且直到我停止程序才停止

import keyboard
import time
import subprocess

while True:
    if keyboard.is_pressed('ctrl+space+b'):
        subprocess.Popen([r"C:\\Program Files\\Everything\\Everything.exe"])
        time.sleep(1.5)

【问题讨论】:

  • keyboard.add_hotkeykeyboard.wait('ctrl+space+b') 怎么样,并添加一个条件来检查您的应用是否打开。

标签: python shortcut


【解决方案1】:

试试这个代码怎么样

import keyboard
import subprocess
import threading

def run_my_program():
    subprocess.run([r"C:\Program Files\Everything\Everything.exe"])

while True:
    if keyboard.is_pressed('ctrl+space+b'):
        threading.Thread(target=run_my_program).start()  # launch up the subprocess in parallel so input is not delayed
        while keyboard.is_pressed('ctrl+space+b'):
            pass  # Wait until user lifts his hands off the keyboard

【讨论】:

    猜你喜欢
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    相关资源
    最近更新 更多