Detailed description of Python Implementation of the function of pressing any key to continue/exit, detailed description of python

Source: Internet
Author: User

Detailed description of Python Implementation of the function of pressing any key to continue/exit, detailed description of python

Preface

To implement this function, you need to pause the program, wait and capture a user's keyboard input, and then continue to execute. Python has a built-in library to help us implement this function, but we need to treat Windows and Linux differently.

msvcrt Ingetch() This method can be used in Windows to obtain a key response and return the corresponding characters. It does not echo in the command line. There are the following sections:

import msvcrtprint ord(msvcrt.getch())

Here we useord Convert the obtained charactersASCII Numeric value. For example, if you capture the key "d" (in lowercase), 100 is returned.

What about Linux? Well, it's a little more complicated, but it's easy to clarify your ideas first.

First, you need to know the three modes of the Linux terminal:Standard Mode,Non-standard modeAndRaw mode:

Standard Mode

Standard mode has also becomecooked Mode is a common mode for users. The characters entered by the driver are stored in the buffer zone and are sent to the program only when the Enter key is received. Buffer data so that the driver can implement the most basic editing functions, the specific keys assigned to these functions are set in the driver, you can use the commandstty Or system calltcsetattr .

Non-standard mode

When the buffer and edit functions are disabled, the connection becomes nonstandard. The terminal processor still processes specific characters, for example, the conversion between Ctrl-C and line breaks, but the edit key is meaningless. Therefore, the corresponding input is considered as a regular data input, the program needs to implement its own editing function.

Raw mode

When all processing is disabled, the driver passes the input directly to the program, and the connection becomesraw Mode.

Here we need to use non-standard mode. to implement similar behavior on Windows, we need the following code:

Import osimport termios # Get standard input descriptor fd = sys. stdin. fileno () # obtain the standard input (terminal) Setting old_ttyinfo = termios. tcgetattr (fd) # configuration terminal new_ttyinfo = old_ttyinfo [:] # use non-standard mode (index 3 is c_lflag or local mode) new_ttyinfo [3] & = ~ Termios. ICANON # disable echo (the input is not displayed) new_ttyinfo [3] & = ~ Termios. ECHO # enable the setting to take effect. termios. tcsetattr (fd, termios. TCSANOW, new_ttyinfo) # read print ord (OS. read (fd, 7) from the terminal ))

From this point of view, we only need to capture a key response using the above method, and then continue the program to achieve the function of pressing any key to continue or exit. Of course, the function of continuing or exiting by pressing 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 [68, 100]:  break

In this way, when the user presses "D" or "d", the program exits.

Summary

The above is all the content for Python to continue or exit by pressing any key. I hope this article will help you learn Python.

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.