粘贴即用,监控键盘
import keyboard
import timedef stopwatch():print('按下S开始计时,E结束计时')start_time = 0running = Falsewhile True:if keyboard.is_pressed('s') and not running:start_time = time.time()running = Trueprint("开始计时")if keyboard.is_pressed('e') and running:elapsed_time = time.time() - start_timerunning = Falseprint(f"统计时间: {elapsed_time:.2f} s")time.sleep(0.1) # 等待一段时间,减少 CPU 使用率stopwatch()