0

My project is to make a program that you can run while you are playing games or other programs in the background. When you press a certain key, your notepad should open and also close after you press the same key again.

I have managed to open notepad with subprocess and that works fine but I have no idea to make it open only when a certain key is pressed. Thanks for any help!

EDIT: What I tried already:

import subprocess
import keyboard

if keyboard.is_pressed('k'):
    subprocess.Popen('C:\\Windows\\System32\\notepad.exe')

input()

here it just doesn't detect any keyboard input, the input() at the end makes the program not close instantly

import subprocess
import keyboard

keyboard.add_hotkey('ctrl+k', print,args=("hello", "test"))

input()

Here if I press "ctrl+k it" will print hello test that means the hotkey works fine. When I switch this part "print,args=("hello", "test")" to "subprocess.Popen('C:\Windows\System32\notepad.exe')"(it should open the program instead of printing hello test) the notepad opens instantly after I run the program and when I press "ctrl+k" I get a big error.

2
  • Hey Dani, welcome. You'll get more luck if you post a minimum reproducible example so people find it easier to see what you've tried and suggest different approaches. Commented Mar 24, 2021 at 8:49
  • @afterburner Okay I will try to reproduce the code I did before. Commented Mar 24, 2021 at 8:51

2 Answers 2

1

A more complex, but still working example could be the following. With this code your program will be always listening the keyboard, not only when you are focused on the input, so may be mre practical in your case

from pynput import keyboard
import subprocess
import threading

class MyException(Exception): pass

class Listening:
    """Is allways waiting for the keyboard input"""
    def __init__(self):
        self.notepad_open = False # to know the state
        with keyboard.Listener(
                on_press=self.on_press) as listener:
            try:
                listener.join()
            except:
                pass
    
    def on_press(self, key):
        try:
            if key.char == "k":
                if not self.notepad_open:
                    self.subprocess = \
                        subprocess.Popen('C:\\Windows\\System32\\notepad.exe')
                    self.notepad_open = True # update state
                else:
                    self.subprocess.kill()
                    self.notepad_open = False # update state
        except: # special key was pressed
            pass

thread = threading.Thread(target=lambda: Listening())
thread.start()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you alot, this is exactly what i tried to do!!
1

The problem is that you check for the key 'k' only once at the beginning. If you want the program to correctly work then you should try this:

import time
import subprocess
import keyboard
while True:
    if keyboard.is_pressed('k'):
        subprocess.Popen('C:\\Windows\\System32\\notepad.exe')
        time.sleep(5)

-I used the time so that you can only open the program once 5 seconds(If you're curious, see what happens without it)-

1 Comment

Thank you! I forgot that it only checks for the k once and without the time.sleep(5) it would just spam open notepad like 100 times. Thanks for the answer, I learned something new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.