Skip to content

Instantly share code, notes, and snippets.

@imcomking
Created August 25, 2016 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imcomking/8059d802b6b00cffb56a635db0a3c2c9 to your computer and use it in GitHub Desktop.
Save imcomking/8059d802b6b00cffb56a635db0a3c2c9 to your computer and use it in GitHub Desktop.
python linux key input
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