Reputation: 27
In the code below, the only way to get out of the loop is by holding q and pressing enter on the last input. But is there a way to check if a key is pressed while any other code is running?
import keyboard, time
while True:
if keyboard.is_pressed('q'):
break
str1 = input('Type your first name: ')
time.sleep(5)
str2 = input('Type your last name: ')
Upvotes: 0
Views: 51
Reputation: 152
I think you want something like these :
import keyboard, time
from threading import Thread
def execute_questions():
while True:
str1 = input('Type your first name: ')
str2 = input('Type your last name: ')
t =Thread(target=execute_questions)
t.daemon = True
t.start()
while True:
if keyboard.is_pressed('q'):
#stops the thread
print('\nprogram finish')
break
Upvotes: 1