Description
Feature or enhancement
We should add a new standard library module, with a function for reading a single key with optional echoing, essentially matching behavior of System.Console.ReadKey
on windows or getch()
on unix.
Rough example:
def getkey(echo: bool = False) -> Key:
...
class Key(enum.Enum):
NONE = 0
BACKSPACE = 8
TAB = 9
...
Usage
from console import getkey, Key
key = getkey()
if key == Key.INSERT:
...
Pitch
Currently this functionality is too simple for most to justify including a library dependency, and tedious or error-prone for self-implementation in projects. It is also fairly short in implementation and not likely to need maintenance after initial implementation.
I think it's reasonable that something as simple as "reading a specific key without printing to terminal" should not require conditional imports of low level windows or C libraries. Currently many projects only support windows or unix just due to this complexity of portable key reading.
Furthermore mscrvt
and termios
's implementations of getch()
returning key bytecodes is obscure for high-level usage like matching keyboard special keys requiring yet another os-specific key mapping implementation. The Key
enum makes it simple to see what keys are available and what their names are.
- I'll be interested in working on a PR for this if possible.
Points of discussion
- The
Key
enum may implement equality with strings to allow comparisons likeif getkey() == 'A'
but I'm unsure at this point if this is more confusing or not. - This module can be implemented in C for a potential GIL release during
getkey
, but I'm unsure if this warrants the added maintenance cost. The performance gain may make it more usable for game libraries.