Python monitoring keyboard input instance code, python monitoring keyboard input
This article focuses on the python monitoring keyboard input code, such as OS, sys, and time. The specific implementation code is as follows:
#! /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 ()
Result:
Summary
The above is all the content of this article on the python monitoring keyboard input instance code, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!