-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop_key.py
More file actions
30 lines (22 loc) · 712 Bytes
/
Copy pathstop_key.py
File metadata and controls
30 lines (22 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import keyboard
import time
import threading
from restart_program import restart_program
def run_with_stop_key(work_function, stop_key='q'):
# Function that does some work
def do_work():
work_function()
# Start the work in a separate thread
work_thread = threading.Thread(target=do_work)
work_thread.start()
# Check for key press to stop the work
while True:
if keyboard.is_pressed(stop_key):
print("\n\033[93mPlease wait... Closing application...\033[0m\n")
time.sleep(1)
restart_program()
break
time.sleep(0.1)
# Wait for the work thread to finish
work_thread.join()
print("Work stopped.")