Python Forum

Full Version: Pausing a loop with spacebar, resume again with spacebard
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
To go back to beginning,so is msvcrt.getch() blocking.
To avoid this use it with msvcrt.kbhit().
Always run from command line when doing stuff like this or stuff(threading) that an editor/IDE can block.
Example:
import time, msvcrt

def stop_me(timeout=5):
   '''None Blocking version'''
   start_time = time.time()
   while True:
       if msvcrt.kbhit():
           key = msvcrt.getch()
           # Python 2 chr(27)
           if key == chr(27).encode():
               print('Esc key pushed')
               return
       if time.time() - start_time > timeout:
           print('Did not manage to stop me in {} sec'.format(timeout))
           return

stop_me()
Pages: 1 2