I did the same thing with termios last time, but I also tried it with tty. [Last time] http://qiita.com/tortuepin/items/9ede6ca603ddc74f91ba
import sys
import termios
import tty
#Get standard input file descriptor
fd = sys.stdin.fileno()
#Get the terminal attributes of fd
old = termios.tcgetattr(fd)
try:
#Switch standard input mode
#Both cbreak and raw don't need enter, but raw is ctrl-c can't be heard??
tty.setcbreak(sys.stdin.fileno())
#tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
#Restore the attributes of fd
termios.tcsetattr(fd, termios.TCSANOW, old)
print(ch)
This one has less freedom than last time, but it's easier.
https://utcc.utoronto.ca/~cks/space/blog/unix/CBreakAndRaw
Recommended Posts