def inputWto(strPrompt,timeOut,mp3Prompt): # purpose: to read a string from the keyboard # Parm1: char str to be used as a prompt # if "" then a "?" is used # Parm2: N the timeOut period (in seconds) # set to 3 sec if <1 or >120 # Parm3: mp3 audio to play while waiting # can be audio/typing_l.mp3 # (not available yet) # returns: # Return1: the string that was read) or "" null String # Return2 returns True (else False if timed out) # NOTE: The "select" function waits for input efficiently # I presume that it sleeps while waiting for timeWait # NOTE: It ONLY works if you click at the # bottom of the RUN window before your keystrokes # source: www.pymotw.com/2/select # Repository: 174.html/IX_package_py_py.txt if strPrompt=="-h" : print("inputWto is a function that accepts keyboard input from") print(" a user") print(" e.g.") print(' reply,isNull =inputWto("prompt",3,"name.mp3"') print if mp3Prompt=="" : pass else : pass # the following sounds are played before the # input is requested (not during) Why??? # uncomment the following to "sound" the typing prompt #control_audioTV("typing_3.mp3") #end if prompt=strPrompt #print("The prompt is supposed to be:") if type(timeOut)==str: timeWait=int(timeOut) if type(timeOut)==int: timeWait=timeOut if ((timeWait<1) or (timeWait>120)): timeWait=7 if prompt=="" : pass #leave an empty prompt unchanged else: # show them how long they have to respond strippedTimeWait=str(timeWait) prompt=prompt+"("+strippedTimeWait+"s):" #end if print(prompt, end='') #accept the response on the same line progName="in function inputWto.py" #by D@CC on 2021BFeb17 tested successfully # changed 2021GJul07 and fully tested # using Thonny on a Raspberry Pi 4B # 2 instances of Thonny cannot run simultaneously # using Terminal pi@raspberrypi:~ $ python3 Wait_Select_Stdin.py #This program waits N seconds for the operator to enter a string. #This program runs (but Times Out) if no Keyboard is plugged in. #This program runs using the RDC (Remote Desktop Connection) keyboard. #print("Click in the RUN window below, then. . . .") #print("You have", timeOut," seconds to type any string (then hit Enter).") rlist, wlist, xlist = select([sys.stdin], [], [], timeWait) # the two [] could contain other input lines e.g. serial ports #print("rlist:",rlist) if rlist: # the thread goes through here if a keyboard is active # meaning it exists but nothing has been typed yet # If nothing is typed, after timeWait seconds # rlist will be empty. The programmer should print # the prompt phrase here #rlist contains garbage but . . . #print("This string was returned from select():",rlist) isKeyBoardActive=True if len(prompt)==0 : prompt="?" bStr=input("") if len(bStr)==0: isKeyBoardActive=False #print("This string was typed on the Key-Board:",bStr) #Enter produced something like this in rlist # else: #print("in func. "select" it Timed Out") isKeyBoardActive=False bStr="" #if end #if isKeyBoardActive: #print("bStr:","'",bStr,"'") #if end #print("program will continue from here.") return bStr, isKeyBoardActive #From: # https://stackoverflow.com/questions/6179537/python-wait-x-secs-for-a-key-and-continue-execution-if-not-pressed # PYthon3 Notes re select() # https://docs.python.org/3/library/select.html #def end inputWto Desktop/Python/inputWto.py #/inputWto.py Desktop/Python/inputWto.py #def end inputWto.py #/inputWto_py.txt