Created
August 25, 2016 06:39
-
-
Save imcomking/8059d802b6b00cffb56a635db0a3c2c9 to your computer and use it in GitHub Desktop.
python linux key input
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 termios, fcntl, sys, os | |
def get_char_keyboard_nonblock(): | |
fd = sys.stdin.fileno() | |
oldterm = termios.tcgetattr(fd) | |
newattr = termios.tcgetattr(fd) | |
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO | |
termios.tcsetattr(fd, termios.TCSANOW, newattr) | |
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) | |
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) | |
c = None | |
try: | |
c = sys.stdin.read(1) | |
except IOError: pass | |
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) | |
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) | |
return c | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment