Search code examples
pythonipythonraw-input

How to make an Interactive CLI based application in python?


I'm writing a python program for our handheld-scanner. We have to scan loads of barcodes, but I don't like to hit 'enter' all the time.

Is there anyway that you can say: while typing, if the string you are typing is exactly 20 chars, create a new input?

I can count the return value of raw_input(), but that's not exactly what I want.


Solution

  • You can use this getch function.

    A small utility class to read single characters from standard input, on both Windows and UNIX systems. It provides a getch() function-like instance.

    Then you can simple use a loop to get 20 chars.

    import sys
    while 1:
        c=''
        while len(c) < 10:
            g = getch()
            sys.stdout.write(g)
            c += g
        print "\nYou said", c