Python command line non-blocking Input

Source: Internet
Author: User

A long time ago, on the windows platform, I wrote a Snake Game in C language. On the cmd interface, I used the kbhit () function to implement non-blocking input.

#! /Usr/bin/python #-*-coding: UTF-8-*-"python non blocking input" "_ author _ = 'zagfai' _ version __= '2017-09-13 'import sysimport selectfrom time import sleepimport termiosimport ttyold_settings = termios. tcgetattr (sys. stdin) tty. setcbreak (sys. stdin. fileno () while True: sleep (. 001) if select. select ([sys. stdin], [], [], 0) = ([sys. stdin], [], []): c = sys. stdin. read (1) if c = '\ x1b': break sys. stdout. write (c) sys. stdout. flush () termios. tcsetattr (sys. stdin, termios. TCSADRAIN, old_settings) print raw_input ('2017 :')

Two modules are used, including termios and tty, to control the input mode of tty, from line input to single character.

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.