代码如下
#!/usr/bin/env python
# coding: utf-8
import os
import sys
import tty
import termios
if __name__ == '__main__':
print 'keybord monitor'
while True:
std = sys.stdin.fileno()
settings = termios.tcgetattr(std)
try:
tty.setraw(std)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(std, termios.TCSADRAIN, settings)
if ch == 'w':
print 'top'
elif ch == 's':
print 'bottom'
elif ch == 'a':
print 'left'
elif ch == 'd':
print 'right'
elif ch == 'q':
print 'exit'
break
elif ord(ch) == 0x03: # ctrl + c
print 'exit'
break