3
votes

I want to detect when the user presses the RETURN/ENTER key. Right now, I am doing this using a while loop, but that blocks my code unless the while is broken.

Is there any way to detect the ENTER press without using while loops? I can't use tkinter either.

1
We'll need more context, what GUI are you using if any? It also sounds like you're looking for non blocking keypress detection.TankorSmash

1 Answers

1
votes

Your question is a little bit ambiguous (as noted by @TankorSmash). But here goes...

from multiprocessing import Process, Pipe

def f(c2):
    count = 1
    while not c2.poll():
        print('hit ENTER to stop ({})'.format(count))
        count += 1

c1, c2 = Pipe()
p = Process(target=f, args=(c2,)) 
p.start()
raw_input()
c1.send(None)
p.join()

My answer still uses while, but it doesn't block the running code. Does this work for you?