Can use
Always run from command line(cmd) when doing stuff like this,or can get unwanted result.
msvcrt.kbhit()
with msvcrt.getch()
then call should be done only when key is pressed.Always run from command line(cmd) when doing stuff like this,or can get unwanted result.
import msvcrt import time def stop_me(timeout=15): '''None Blocking version''' start_time = time.time() while True: if msvcrt.kbhit(): key = msvcrt.getch() if key == chr(27).encode(): return 'Esc key pushed' if time.time() - start_time > timeout: return f'Did not manage to stop me in {timeout} sec' if __name__ == '__main__': print(stop_me())