Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do "hit any key" in python?

Tags:

python

How would I do a "hit any key" (or grab a menu option) in Python?

  • raw_input requires you hit return.
  • Windows msvcrt has getch() and getche().

Is there a portable way to do this using the standard libs?

like image 232
Nick Avatar asked Sep 08 '09 16:09

Nick


2 Answers

try:     # Win32     from msvcrt import getch except ImportError:     # UNIX     def getch():         import sys, tty, termios         fd = sys.stdin.fileno()         old = termios.tcgetattr(fd)         try:             tty.setraw(fd)             return sys.stdin.read(1)         finally:             termios.tcsetattr(fd, termios.TCSADRAIN, old) 
like image 109
John Millikin Avatar answered Sep 29 '22 04:09

John Millikin


try:   os.system('pause')  #windows, doesn't require enter except whatever_it_is:   os.system('read -p "Press any key to continue"') #linux 
like image 24
4 revs Avatar answered Sep 29 '22 02:09

4 revs