raw_keyboard.py

来自「Most satellite dish actuators send a ser」· Python 代码 · 共 50 行

PY
50
字号
# Get the keyboard into raw mode.import termios, sys, os,fcntldef UnBlock(fd) :  fl = fcntl.fcntl(fd, fcntl.F_GETFL, 0)  fl |= os.O_NONBLOCK  fcntl.fcntl(fd, fcntl.F_SETFL, fl)class UnixKeyboard :  "Raw input for the duration of the importer"  def __init__(self) :    self.fd = sys.stdin.fileno()    UnBlock(self.fd)    self.old = termios.tcgetattr(self.fd)    new = termios.tcgetattr(self.fd)    new[3] = new[3] & ~termios.ICANON & ~termios.ECHO    new[6][termios.VMIN] = 1    new[6][termios.VTIME] = 0    termios.tcsetattr(self.fd, termios.TCSANOW, new)  def getch(self):    ret = ""    try :      ret = os.read(self.fd, 1)    except OSError, info :      if info.errno==11 :        pass      else :        raise sys.exc_type, sys.exc_value    return ret  def __del__(self) :    import termios    termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)Keyboard = UnixKeyboard()if __name__ == "__main__" :    print "Running key echo program"  while True :    b = Keyboard.getch()    sys.stdout.write(b)    sys.stdout.flush()    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?