Facebook
From Coral Lechwe, 2 Years ago, written in Python for S60.
Embed
Download Paste or View Raw
Hits: 71
  1. import _thread
  2. import time
  3. import sys
  4. import os
  5.  
  6. class _Getch:
  7.     """Gets a single character from standard input.  Does not echo to the screen."""
  8.     def __init__(self):
  9.         try:
  10.             self.impl = _GetchWindows()
  11.         except ImportError:
  12.             self.impl = _GetchUnix()
  13.     def __call__(self): return self.impl()
  14.  
  15. class _GetchUnix:
  16.     def __init__(self):
  17.         import sys, tty, termios
  18.     def __call__(self):
  19.         import sys, termios
  20.         fd = sys.stdin.fileno()
  21.         old_settings = termios.tcgetattr(fd)
  22.         try:
  23.             tty.setraw(sys.stdin.fileno())
  24.             ch = sys.stdin.read(1)
  25.         finally:
  26.             termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  27.         return ch
  28.  
  29. class _GetchWindows:
  30.     def __init__(self):
  31.         import msvcrt
  32.     def __call__(self):
  33.         import msvcrt
  34.         msvcrt_char = msvcrt.getch()
  35.         return msvcrt_char.decode("utf-8")
  36.  
  37. def input_thread(done):
  38.     getch = _Getch()
  39.     while getch.impl() != 'q': pass
  40.     done[0] = True
  41.  
  42. def main():
  43.     done = [False]
  44.     _thread.start_new_thread(input_thread, (done,))
  45.     i = 0
  46.     while not done[0]:
  47.         i+=1
  48.         #do your things here
  49.         print("\r"+['\\','|','/','-'][i&3]+" press q to quit...", end="")
  50.        time.sleep(.1)
  51.    print()
  52.  
  53. main()
  54.