I have a function which takes around 10 seconds to run. I want it to run over and over again until I press a certain key, then finish up the function.
I could use the python keyboard
module to detect if a key is pressed, but if I press a key while the function is running, it won’t detect that. Is there a function that put the keys pressed in a queue, which I could unload every time the function ends?
I’ve tried this code:
import pygame
import time
screen = pygame.display.set_mode((500, 500))
pygame.init()
def f():
time.sleep(10)
print('done')
running = True
while running:
f()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
running = False
but that only works when q is pressed in the pygame window