I have been trying out (for my own personal use) some peoples' solutions to timed keyboard inputs and the only one that has worked was one by Alex Martelli/martineau here. I used their second block of code (starting with import msvcrt) and it worked great for pretty much everything but comparisons. I replaced the return of None with an empty string if no input is entered in time and I used some test lines as shown below:
import msvcrt
import time
def raw_input_with_timeout(prompt, timeout):
print prompt,
finishat = time.time() + timeout
result = []
while True:
if msvcrt.kbhit():
result.append(msvcrt.getche())
if result[-1] == '\r': # or \n, whatever Win returns;-)
return ''.join(result)
time.sleep(0.1) # just to yield to other processes/threads
else:
if time.time() > finishat:
return ""
textVar = raw_input_with_timeout("Enter here: \n", 5)
print str(textVar) # to make sure the string is being stored
print type(str(textVar)) # to make sure it is of type string and can be compared
print str(str(textVar) == "test")
time.sleep(10) # so I can see the output
After I compile that with pyinstaller, run it, and type test into the window, I get this output:
Enter here:
test
test
<type 'str'>
False
I originally thought the comparison was returning False because the function appends characters to an array and that may have had something to do with it not doing a proper comparison with a string, but after looking further into the way Python works (namely, SilentGhost's response here), I really have no idea why the comparison will not return True. Any response is appreciated. Thank you!