How to do “hit any key” in python?

后端 未结 7 1830
予麋鹿
予麋鹿 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:50

    A couple years ago I wrote a small library to do this in a cross-platform way (inspired directly by John Millikin's answer above). In addition to getch, it comes with a pause function that prints 'Press any key to continue . . .':

    pause()
    

    You can provide a custom message too:

    pause('Hit any key')
    

    If the next step is to exit, it also comes with a convenience function that calls sys.exit(status):

    pause_exit(status=0, message='Hit any key')
    

    Install with pip install py-getch, or check it out here.

提交回复
热议问题