Python implements the ability to press any key to continue/exit

Source: Internet
Author: User
Preface

To do this, you need to pause the program, wait and catch a keyboard input from the user, and then continue. Python has built-in libraries to help us with this functionality, but we want to differentiate between Windows and Linux.

msvcrt The getch() method in can help implement under Windows, which is to get a key response and return the corresponding character. It is not echoed in the command line. There are the following program segments:

Import Msvcrtprint Ord (Msvcrt.getch ())

This takes advantage ord of converting the obtained character to a ASCII numeric value, such as capturing the key "D" (note is lowercase) to get the value 100.

What about Linux? Well, relatively complex a little bit, but first clear the idea of the words will be done.

The first thing to know about the Linux terminal three modes, respectively, is the canonical mode , non-canonical mode and raw mode :

Canonical mode

Canonical mode, also being a pattern cooked , 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 allows the driver to implement the most basic editing functions, the specific keys assigned to these functions are set in the driver, and can be stty modified by commands or system calls tcsetattr .

Non-canonical mode

When the buffering and editing functions are turned off, the connection becomes non-canonical mode. The terminal processor still carries out specific character processing, such as processing transitions between ctrl-c and newline characters, but the edit key is meaningless, so the input is treated as a regular data entry, and the program 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 becomes a raw pattern.

Here we need to use non-canonical mode, then to implement the similar behavior on Windows just now, you need the following code:

Import Osimport Termios # Get the standard input descriptor FD = Sys.stdin.fileno () # Get the settings for standard input (terminal) Old_ttyinfo = Termios.tcgetattr (FD) # Configure terminal new_t Tyinfo = old_ttyinfo[:] # Use non-canonical mode (index 3 is c_lflag, local mode) new_ttyinfo[3] &= ~termios. Icanon # Turn off echo (input will not be displayed) New_ttyinfo[3] &= ~termios. ECHO # makes the setting effective termios.tcsetattr (FD, Termios. Tcsanow, New_ttyinfo) # reads print ord from the terminal (Os.read (FD, 7))

As a result, we simply use the method above to capture a key response, and then continue the program to press any key to continue or exit the function. Of course, the ability to continue or exit by the specified key can also be implemented in a similar way, for example:

Import MSVCRT Print ("Press ' D ' to exit ...") while True:if Ord (Msvcrt.getch ()) in [, +]: Break  

This way, the program exits when the user presses "D" or "D".

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.