Python Forum
Pausing a loop with spacebar, resume again with spacebard
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pausing a loop with spacebar, resume again with spacebard
#11
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to create one resume parser api using Python Flask parthsukhadiya 1 839 Nov-28-2023, 05:07 PM
Last Post: Pedroski55
  Pausing and returning to function? wallgraffiti 1 2,159 Apr-29-2021, 05:30 PM
Last Post: bowlofred
Question resume file transfer onran 0 1,632 Jan-27-2021, 02:16 PM
Last Post: onran
  Slide show with mouse click pausing aantono 1 2,190 Jan-28-2020, 04:25 AM
Last Post: Larz60+
  Pausing a running process? MuntyScruntfundle 2 7,240 Nov-14-2018, 05:14 PM
Last Post: woooee
  Pausing a Script malonn 4 3,813 Aug-04-2018, 07:58 PM
Last Post: malonn
  How to Make Python code execution pause and resume to create .csv and read values. Kashi 2 3,753 Jun-14-2018, 04:16 PM
Last Post: DeaD_EyE
  Pausing and playing a while loop. traceon 1 3,879 Jan-24-2017, 09:11 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020