Learn how to effectively stop a running Python program by pressing a key using the `pynput` library. This guide will show you step-by-step how to implement this functionality.
---
This video is based on the question https://stackoverflow.com/q/72793409/ asked by the user 'tad' ( https://stackoverflow.com/u/16511036/ ) and on the answer https://stackoverflow.com/a/72947714/ provided by the user 'tad' ( https://stackoverflow.com/u/16511036/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Stopping a python program when an arbitrary key was pressed while the program is running
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Stop a Python Program with a Key Press Using pynput
Have you ever been running a Python script that you needed to stop quickly, perhaps with the press of a key? Stopping an ongoing program can be quite important, whether for testing purposes or avoiding unwanted results. In this guide, we'll explore how to achieve this using the pynput library.
The Problem: Stopping a Running Python Program
Many programmers often encounter the situation where they need to terminate a Python program while it's still running. A common approach is to listen for specific key presses that can interrupt the running process. The signal module is often used to catch interrupts like SIGINT, but it has its limitations—especially when you want more control over which keys can trigger the stop command.
The Solution: Using pynput for Key Press Detection
The pynput library allows you to monitor keyboard input in an easy manner, enabling more interactive control over running Python programs. Below, I'll explain how to set it up correctly to achieve the desired effect of a program that can stop based on key presses.
Step 1: Install pynput
First, if you haven't already, you need to install the pynput library. You can do so using pip:
[[See Video to Reveal this Text or Code Snippet]]
Step 2: Implementing the Listener
Here's a simple implementation to get you started. We'll create a program that runs a loop and can be stopped by pressing the key 'q'.
[[See Video to Reveal this Text or Code Snippet]]
Understanding the Code
Importing Libraries: We start by importing necessary libraries: pynput for keyboard input, os for exiting the program, threading for running the loop on a separate thread, and time for adding delays.
Global Variable: We declare loop_running to control whether the loop should keep running or stop based on key presses.
Key Press Function: The on_press function captures keyboard events. If 's' is pressed, it starts the loop in a new thread. If 'q' is pressed, it sets loop_running to False, which will stop the loop.
Looping Function: The start_loop function contains the loop where we print numbers from 0 to 99. It checks the loop_running variable during each iteration—if loop_running is False, the program safely exits.
Listening: Finally, we set up a listener that will monitor key presses and execute the on_press function whenever a key is pressed.
Conclusion
Using the pynput library, you can easily manage keyboard inputs and stop running Python programs based on those inputs effectively. This approach not only enhances the control you have over your programs but also enriches the overall user experience in terminal applications.
Now, you can implement this technique in your projects to make your Python applications more interactive and easy to manage. Happy coding!
On this page of the site you can watch the video online How to Stop a Python Program with a Key Press using pynput with a duration of hours minute second in good quality, which was uploaded by the user vlogize 05 April 2025, share the link with friends and acquaintances, this video has already been watched 51 times on youtube and it was liked by like viewers. Enjoy your viewing!