我想检测用户何时按下 RETURN/ENTER 键。现在,我正在使用 while 循环执行此操作,但这会阻止我的代码,除非 while 被破坏。
有什么方法可以在不使用 while 循环的情况下检测 ENTER 按下?我也不能使用 tkinter。
您的问题有点模棱两可(如@TankorSmash 所述)。但是这里...
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()
我的答案仍然使用while
,但它不会阻止正在运行的代码。这对你有用吗?