How to do “hit any key” in python?

后端 未结 7 1831
予麋鹿
予麋鹿 2020-12-01 09:00

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().
  • <
7条回答
  •  旧巷少年郎
    2020-12-01 09:47

    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)
    

提交回复
热议问题