我想检测用户何时按下 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
,但是不会阻塞运行的代码。这对你有用吗?
关于python - 检测按键而不阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18240016/