Search code examples
pythonshortcut

Why my app keep opening when I use shortcut in Python?


I tried to create a program to open a app with a specific shortcut but when I press my keys it keep opening and didn't stop until I stopped the program

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)


Solution

  • How about try this code

    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