Linux under Python Press any key to exit the method

Source: Internet
Author: User
One day in the group of classmates asked, in Python I use input or raw_input all have to input after enter to get to enter the value, that how to implement any key exit pause and other functions, I did not think, because the contact Python time is not too long, mainly Linux under.

Of course, the Windows system will be a little easier under the Windows system if you install a Python environment, the default comes with a module called Msvcrt,import MSVCRT, and then call Msvcrt.getch (). Next, Linux implements the Python version by pressing any key to exit.

Beginner python in the total want to implement a press any key to continue/exit program (subject to. bat poison), but still can't write out, recently learned UNIX C can be realized through the Termios.h library, try to find Python also has this library, so finally write a program like this. Here's the code:

#!/usr/bin/env python#-*-coding:utf-8-*-import osimport sysimport termiosdef press_any_key_exit (msg): # Gets the descriptor of the standard input FD = Sys.stdin.fileno () # Get standard input (terminal) Settings old_ttyinfo = Termios.tcgetattr (FD) # configure terminal New_ttyinfo = old_ttyinfo[:]# use non-canonical mode (index 3 is C _lflag is local mode) new_ttyinfo[3] &= ~termios. icanon# Turn off echo (input is not displayed) New_ttyinfo[3] &= ~termios. echo# Output Information Sys.stdout.write (msg) Sys.stdout.flush () # makes the setting effective termios.tcsetattr (FD, Termios. Tcsanow, New_ttyinfo) # reads Os.read from terminal (FD, 7) # Restore terminal Settings termios.tcsetattr (FD, Termios. Tcsanow, old_ttyinfo) if __name__ = = "__main__":p ress_any_key_exit ("Press any key to continue ...") Press_any_key_exit ("Press any key to exit ...")

For additional information about Termios, refer to the Linux manual:

Mans 3 Termios

Add another three modes of the *nix terminal (excerpt from the <unix-linux Programming Practice Tutorial >)

Canonical mode

Canonical mode, also known as cooked mode, is a common pattern for users. The driver enters the word characters in the buffer and sends the buffered characters to the program only when the ENTER key is received. Buffered data enables the driver to implement the most basic editing functions, and the specific keys assigned to these functions are set in the driver , can be modified by command Stty or system call tcsetattr

Non-canonical mode

When the buffering and editing functions are turned off, the connection becomes non-canonical. The terminal processor continues to perform specific character processing, such as converting between ctrl-c and newline characters, but the edit key will not make sense, so the input is considered a regular data-entry program that needs to implement its own editing capabilities

Raw mode

When all processing is turned off, the driver passes the input directly to the program, and the connection is made into raw mode.

The above is a small series to introduce you to the Linux implementation of Python under any key to exit the implementation method, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your topic.alibabacloud.com support here!

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.