您好,登錄后才能下訂單哦!
本文研究的主要是python監(jiān)控鍵盤輸入的相關(guān)代碼,用到了os,sys,time等,具體實(shí)現(xiàn)代碼如下:
#!/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: #這個(gè)是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()
結(jié)果:
以上就是本文關(guān)于python監(jiān)控鍵盤輸入實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。