pythonkeyboardtext-based

How to block keyboard input in Python 2.7?


I'm currently creating a text-based RPG in Python.

When the game is first launched, it prints a small introduction message letter by letter. The thing is that, while it's printing, the user can still type on the keyboard and insert random letters in the text.

How can I block the keyboard input while the introduction is being printed ?

Here's the letter-by-letter printing function:

def slow_print(txt, duration=1):
    delay = float(duration)/len(txt)

    #Block input    

    for c in txt:
        write(c)
        time.sleep(delay)
    print

    #Unblock input

And the write function:

def write(s):
    sys.stdout.write(s)
    sys.stdout.flush()

Note: I'm on Linux


Solution

  • I think you're asking about turning off the "echoing" of keyboard to the console. not sure how cross-platform support for control of this, but the standard termios module lets you do this in Posix systems.

    there's even an example in the docs doing this