本文研究的主要是python监控键盘输入的相关代码,用到了os,sys,time等,具体实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import tty, termios import time if __name__ = = '__main__' : print "Reading form keybord" print """ i j k l m""" print 'press Q to quit' while True : fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) #old_settings[3]= old_settings[3] & ~termios.ICANON & ~termios.ECHO try : tty.setraw(fd) ch = sys.stdin.read( 1 ) finally : termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) #print 'error' if ch = = 'i' : print 'move forward' elif ch = = 'm' : print 'move back' elif ch = = 'j' : print "turn left!" elif ch = = 'l' : print "turn right!" elif ch = = 'u' : print "turn right!" elif ch = = 'o' : print "turn right!" elif ch = = 'k' : print "stop motor!" elif ch = = 'q' : print "shutdown!" break elif ord (ch) = = 0x3 : #这个是ctrl c print "shutdown" break print "Reading form keybord" print """ i j k l m""" print 'press Q or ctrl+c to quit' #rate.sleep() |
结果:
总结
以上就是本文关于python监控键盘输入实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://blog.csdn.net/u010918541/article/details/54709222