I used keyboard module to implement a keypress event detector for a code. I need to detect the keypress event inside a while loop inside a for loop. The code is as follows
import keyboard
for i in range(5):
loop = True
while loop:
if keyboard.is_pressed("space"):
print("Iteration: {}\tSpace key pressed!".format(i))
loop = False
The output shows after pressing space key once:
Iteration: 0 Space key pressed!
Iteration: 1 Space key pressed!
Iteration: 2 Space key pressed!
Iteration: 3 Space key pressed!
Iteration: 4 Space key pressed!
I want it to detect only when the key is pressed. is_pressed sets it to true forever once the key is pressed. Is there any other way to get it detected only once and reset the is_pressed to false?