Python monitoring keyboard input instance code, python monitoring keyboard input

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.