Forum: Poser Python Scripting


Subject: Breaking a SimpleXMLRPCServer with a keystroke

FVerbaas opened this issue on Dec 10, 2020 ยท 40 posts


adp001 posted Sat, 12 December 2020 at 12:56 AM

Found something on stackoverflow.com that might be helpful. Can*t test it myself, because I'm not on Windows.

Keyboard interrupt after 5 seconds:

import sys, time, msvcrt

def readInput( caption, default, timeout = 5):
    start_time = time.time()
    sys.stdout.write('%s(%s):'%(caption, default));
    input = ''
    while True:
        if msvcrt.kbhit():
            chr = msvcrt.getche()
            if ord(chr) == 13: # enter_key
                break
            elif ord(chr) >= 32: #space_char
                input += chr
        if len(input) == 0 and (time.time() - start_time) > timeout:
            break

    print ''  # needed to move to next line
    if len(input) > 0:
        return input
    else:
        return default