I'm trying to write a program that needs to detect key presses, which I've already done using this code I found online:
TERMIOS = termios
def getkey():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = ''
try:
c = repr(sys.stdin.read(1))
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c
But I can't seem to find any good way to make it send back a string ("no keypress" or something like that, for example) if it doesn't detect any key press within a certain time period. Anyone have any ideas? I'm using python 2.7.3 on ubuntu 12.03. Thanks.