Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like getch()
). I know there's a function in Windows for it, but I'd like something that is cross-platform.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I believe that this is one the most elegant solution.
and then use it in the code:
Try this with pygame:
My solution for python3, not depending on any pip packages.
An alternative method:
From this blog post.
The
curses
package in python can be used to enter "raw" mode for character input from the terminal with just a few statements. Curses' main use is to take over the screen for output, which may not be what you want. This code snippet usesprint()
statements instead, which are usable, but you must be aware of how curses changes line endings attached to output.This code, based off here, will correctly raise KeyboardInterrupt and EOFError if Ctrl+C or Ctrl+D are pressed.
Should work on Windows and Linux. An OS X version is available from the original source.