I have this function that will run for around an hour or so. I need a way to check for the keypress constantly while the store_data() function is running. The current code gives me a very small time frame to press the combination. Is there a way to keep checking for the keypress? The for loop goes on for like an hour or two and I need to constantly check for the keypress WHILE the function is being executed. I could use ctrl + c, but I dont want that. I want to raise a keyboard interrupt error when a certain key combination is pressed. Thank you
import keyboard
import time
def store_data():
dict_of_data = []
excel_file = read_excel()
for word in excel_file:
try:
if keyboard.is_pressed('ctrl + alt + o'):
print("pressed")
raise KeyboardInterrupt("key interrupted")
word_data = another_func(word)
time.sleep(1)
except KeyboardInterrupt:
open_excel() #another func that i need to call if "ctrl + alt + o" is pressed and pause the current function
word_data = another_func(symbol)
pass
dict_of_data.append(word_data)
return dict_of_data