I am writing a code that takes a user inputted word or phrase, and then returns a string of the 50 words before and after the given word. I want it to be able to scroll through the instances where that word or phrase is found by pressing the space bar to continue, or any other button to quit. The problem that I'm running into is that if I use the keyboard.wait() method I can only allow for one type of user input, but if I use any of the other keyboard methods that I have tried it doesn't stop my while loop and accept input. Here is the code I'm currently working with, any ideas would be helpful.
while True:
keyboard.wait(" "):
x = (x+1)%len(where_found)
context = contents[where_found[x]-50:where_found[x]+50]
context = " ".join(context)
print(context+"\n")
where_found is a list containing all the index positions of the word that was searched for, or the first word if it was a phrase. content is a list created with the read().split() method of file reading. x is assigned a value of 1 before this section of code and allows the code to cycle through the instances where it was found.
Edit: I have tried both readchar.readkey() and msvcrt.getch() with no success. When they are reached my shell responds as if it is waiting for an input, but never actually accepts one. I am using windows and python 3.8.5.