Implement the Python version Press any key to continue/exit _python

Source: Internet
Author: User
Tags in python

One day in the group, some students asked, in Python I use input or raw_input have to enter after the input value, how to achieve any key out of the pause function, I did not think much, because the contact Python time is not long, mainly under Linux.

To implement this feature, you need to pause the program, wait and capture a user's keyboard input, and then continue execution. Python has built-in libraries that can help us implement this functionality, but treat Windows and Linux differently.

Of course, Windows system will be a little bit simpler, Windows system if you install the Python environment, the default is a module called Msvcrt,import MSVCRT, and then call Msvcrt.getch () can be.

1, press ENTER to exit.

#coding =utf-8
raw_input Unicode (' press ENTER to exit ... ', ' utf-8 '). Encode (' GBK '))

2, press any key to continue.

Import OS
os.system (' pause ')

Next is Linux to implement the Python version of the Press any key to exit.

Beginners in Python always want to implement a press any key to continue/quit the program (by the. bat poison), but has been written, the recent study of Unix C found can be implemented 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 os
import sys
import termios

def press_any_key _exit (msg):
  # Get 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[:]

  # using nonstandard mode (index 3 is C_lflag is native mode)
  new_ttyinfo[3] &= ~ Termios. Icanon
  # Close echo (input will not be displayed)
  new_ttyinfo[3] &= ~termios. ECHO

  # Output Information
  sys.stdout.write (msg)
  Sys.stdout.flush ()
  # causes settings to take effect
  termios.tcsetattr (FD, Termios. Tcsanow, New_ttyinfo)
  # read from the terminal
  Os.read (FD, 7)

  # Restore terminal settings
  termios.tcsetattr (FD, Termios. Tcsanow, Old_ttyinfo)

if __name__ = = "__main__":
  press_any_key_exit ("Press any key to continue ...")
  Press_any_key_exit ("Press any key to exit ...")

Other information about Termios can be referenced in the Linux manual:

man 3 termios

Additional three modes of *nix terminal (excerpt from <unix-linux programming Practice Tutorial >)

Canonical mode

Canonical mode, also become cooked mode, is a common pattern for users. The characters entered by the driver are Fu Paocun in the buffer and are sent to the program only when the return key is received. Buffering data enables the driver to implement the most basic editing functions, and the specific keys assigned to these functions are set in the driver , you can modify it by command Stty or system call tcsetattr

Non-canonical mode

When Buffering and editing are turned off, the connection becomes unmanaged. The terminal processor still carries out certain character processing, such as processing ctrl-c and newline characters, but the edit key will be meaningless, so the corresponding input is considered a regular data entry program that needs to be edited.

Raw mode

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

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.