Created
March 5, 2013 00:28
-
-
Save davstott/5086982 to your computer and use it in GitHub Desktop.
Linux python character mode stdin instead of line mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, fcntl, os, termios, tty | |
def init(): | |
#take the current settings | |
fnctlSettings = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) | |
termiosSettings = termios.tcgetattr(sys.stdin.fileno()) | |
# set the new ones | |
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, os.O_NONBLOCK | fnctlSettings) | |
tty.setraw(sys.stdin.fileno()) | |
return [fnctlSettings, termiosSettings] | |
if __name__ == '__main__': | |
originalSettings = init() | |
try: | |
#do Stuff | |
finally: | |
#put the original settings back, even if we've had an exception | |
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, originalSettings[0]) | |
termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, originalSettings[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment