# input_without_enter_with_timeout_python3.py
# lire une séquence de chaînes validée quand rien pressé pendant timeout secondes (0.5 seconde ici)
# https://stackoverflow.com/questions/28285687/keyboard-input-with-timeout-and-without-press-enter
import sys
from select import select
import tty
import termios

try:
    # more correct to use monotonic time where available ...
    from time33 import clock_gettime
    def time(): return clock_gettime(0)
except ImportError:
    # ... but plain old 'time' may be good enough if not.
    from time import time

def input_with(timeoutdefault):
    """Read an input from the user or timeout"""
    sys.stdout.flush()    
    # store terminal settings
    old_settings termios.tcgetattr(sys.stdin)
    buff ''
    try:    
        tty.setcbreak(sys.stdin# flush per-character
        break_time time() + timeout
        while True:
            rlist_select([sys.stdin], [], [], break_time time())
            if rlist:
                sys.stdin.read(1)
                # swallow CR (in case running on Windows)
                if == '\r':
                    continue
                # newline EOL or EOF are also end of input
                if in ('\n'None'\x04'):
                    break # newline is also end of input
                buff += c
                sys.stdout.flush()
            else# must have timed out
                break
    finally:
        # put terminal back the way it was
        termios.tcsetattr(sys.stdintermios.TCSADRAINold_settings)
    if buff:
        return str(buff.replace('\n','').strip())
    else:
        return default
        
def getSequ(prompt1):
    """Attendre une séquence de touches du user"""
   print('[getSequ] '+prompt1)
   sek ''
   timeout 0.5  # augmenter pour personnes lentes
   default ''
   while True:
      gk_touche input_with(timeoutdefault)
      if sek == '':
         if gk_touche != '':
            sek str(gk_touche)
      else:
         if gk_touche == default:
            break
         else:
            sek sek str(gk_touche)
   return sek
   
getSequ("Sequence svp")
print("x=" x)