溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python監(jiān)控鍵盤輸入實(shí)例代碼

發(fā)布時(shí)間:2020-09-05 18:19:18 來(lái)源:腳本之家 閱讀:177 作者:CSDN_Flying 欄目:開發(fā)技術(shù)

本文研究的主要是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é)果:

python監(jiān)控鍵盤輸入實(shí)例代碼

總結(jié)

以上就是本文關(guān)于python監(jiān)控鍵盤輸入實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

向AI問一下細(xì)節(jié)

免責(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)容。

AI