python鍵盤監聽

代碼如下

#!/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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章