Achieve python version of the press any key to continue and exit

  • 2020-05-12 02:51:29
  • OfStack

One day, some students in the group asked, "under python, I have to enter input or raw_input to get the input value. Then how to achieve any key exit and pause functions?" I did not think much at that time, because the contact time of python was not long, mainly under Linux.

To do this, all you need to do is pause the program, wait for and capture a user's keyboard input, and then continue. Python has built-in libraries to help us do this, but make a distinction between Windows and Linux.

Of course, Windows is a little simpler. If you install python, Windows has a module called msvcrt, import msvcrt, and then msvcrt.getch ().

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')

The next step is to press any key to exit the python version under Linux.

When I was just beginning to learn Python, I always wanted to achieve a program to press any key to continue/exit (under the.bat poison), but I couldn't write it. When I recently learned Unix C, I found that it could be realized through termios.h library.


#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
import termios

def press_any_key_exit(msg):
  #  Gets the descriptor for standard input 
  fd = sys.stdin.fileno()

  #  Get standard input ( terminal ) The setting of the 
  old_ttyinfo = termios.tcgetattr(fd)

  #  Configure the terminal 
  new_ttyinfo = old_ttyinfo[:]

  #  Use non-canonical patterns ( The index 3 is c_lflag  That's the local mode )
  new_ttyinfo[3] &= ~termios.ICANON
  #  Close the echo ( The input will not be displayed )
  new_ttyinfo[3] &= ~termios.ECHO

  #  Output information 
  sys.stdout.write(msg)
  sys.stdout.flush()
  #  Enable Settings 
  termios.tcsetattr(fd, termios.TCSANOW, new_ttyinfo)
  #  Read from terminal 
  os.read(fd, 7)

  #  Restore terminal setting 
  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 found in the Linux manual:

man 3 termios

In addition, there are 3 modes of *nix terminal (excerpted) < Unix-Linux programming practice tutorial > )

Specification model

Specification model, also known as cooked mode, is a common pattern of the user. The driver input of characters stored in buffer, and only when receives the enter key to send these buffer character to the program. Data buffer so drivers can achieve the most basic editing features, was assigned to these functions of certain key setting in the driver, you can through the command to invoke tcsetattr stty or system changes

Non-canonical mode

When buffering and editing functions are turned off, the connection is made into non-canonical mode. The terminal processor still performs specific character processing, such as the conversion between Ctrl-C and newline characters, but the edit key will not be meaningful, so the corresponding input is treated as normal data entry and the program needs to implement its own editing function

raw mode

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


Related articles: