#๐Ÿ”’ Stop loop by listening to background key press

4 messages ยท Page 1 of 1 (latest)

viral dome
#

Target:
Stop my program by listening for a key (i.e. the Esc key), so the program could be stopped without opening the cmd.

My approach:
I tried leveraging a thread using pynput.keyboard.listener and its on_press callback that would receive a threading.Event, setting it when Esc is pressed. This would control a while loop: while not exit_event.is_set(). This loop holds the logic for my whole app. Exiting the loop, I need to free some resources using del, joining other threads and handle some other stuff before exiting, so I can't just use sys.exit.

Any clue on how could I send any signal to this loop to immediately stop?

quaint chasmBOT
#

@viral dome

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quaint chasmBOT
#

@viral dome

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.