如何在循环期间的任何时候检查按键?
How can I check for a key press at any point during a loop?
我正在尝试制作一个倒计时到 0,然后开始递增的计时器。我正在使用时间和键盘模块。
来自 PyPi 的 keyboard 模块。
一切都按预期工作,我可以按一个按钮关闭程序,但它只在每次迭代开始时有效。有没有办法让它在循环为 运行 时随时检查按键?我需要使用不同的模块吗?
这是我的代码:
import time
import keyboard
m = 2
s = 0
count_down = True
while True:
if keyboard.is_pressed('q'):
break
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
在这种情况下使用回调是常见的方法,这里是解决方案:
import time
import keyboard
m = 2
s = 0
count_down = True
break_loop_flag = False
def handle_q_button():
print('q pressed')
global break_loop_flag
break_loop_flag = True
keyboard.add_hotkey('q', handle_q_button)
while True:
if break_loop_flag:
break
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1q
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
如果你想同时做任何两件事,独立于另一件事,你需要考虑使用 multiprocessing
。然而,即使你这样做了,你的循环要么仍然需要检查一个密钥是否已经在另一个进程中注册,要么你需要终止进程 运行 强行循环,这可能会导致意想不到的结果。
但是,在你的情况下,由于没有像写入文件这样的副作用,所以这会起作用:
import time
import keyboard
from multiprocessing import Process
def print_loop():
m = 2
s = 0
count_down = True
while True:
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
def main():
p = Process(target=print_loop)
p.start()
# this loop runs truly in parallel with the print loop, constantly checking
while True:
if keyboard.is_pressed('q'):
break
# force the print loop to stop immediately, without finishing the current iteration
p.kill()
if __name__ == '__main__':
main()
我正在尝试制作一个倒计时到 0,然后开始递增的计时器。我正在使用时间和键盘模块。 来自 PyPi 的 keyboard 模块。
一切都按预期工作,我可以按一个按钮关闭程序,但它只在每次迭代开始时有效。有没有办法让它在循环为 运行 时随时检查按键?我需要使用不同的模块吗?
这是我的代码:
import time
import keyboard
m = 2
s = 0
count_down = True
while True:
if keyboard.is_pressed('q'):
break
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
在这种情况下使用回调是常见的方法,这里是解决方案:
import time
import keyboard
m = 2
s = 0
count_down = True
break_loop_flag = False
def handle_q_button():
print('q pressed')
global break_loop_flag
break_loop_flag = True
keyboard.add_hotkey('q', handle_q_button)
while True:
if break_loop_flag:
break
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1q
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
如果你想同时做任何两件事,独立于另一件事,你需要考虑使用 multiprocessing
。然而,即使你这样做了,你的循环要么仍然需要检查一个密钥是否已经在另一个进程中注册,要么你需要终止进程 运行 强行循环,这可能会导致意想不到的结果。
但是,在你的情况下,由于没有像写入文件这样的副作用,所以这会起作用:
import time
import keyboard
from multiprocessing import Process
def print_loop():
m = 2
s = 0
count_down = True
while True:
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
def main():
p = Process(target=print_loop)
p.start()
# this loop runs truly in parallel with the print loop, constantly checking
while True:
if keyboard.is_pressed('q'):
break
# force the print loop to stop immediately, without finishing the current iteration
p.kill()
if __name__ == '__main__':
main()