python-readchar
Library to easily read single chars and keystrokes.
Born as a python-inquirer requirement.
Installation
simply install it via pip
:
pip install readchar
or download the source code from PyPi.
Usage
Usage example:
import readchar
c = readchar.readchar()
key = readchar.readkey()
API
There are just two methods:
readchar(blocking=True) -> str | None
Reads the next char from stdin
, returning it as a string with length 1.
By default, it will block until a character is available, but if you pass blocking=False
the function will not block and return None
if no character is available.
readkey() -> str
Reads the next keystroke from stdin
, returning it as a string. Waits until a keystroke is available.
A keystroke can be:
- single characters as returned by
readchar()
. These include:- character for normal keys: 'a', 'Z', '9'...
- special characters like 'ENTER', 'BACKSPACE', 'TAB'...
- combinations with 'CTRL': 'CTRL'+'A',...
- keys that are made up of multiple characters:
- characters for cursors/arrows: 🡩, 🡪, 🡫, 🡨
- navigation keys: 'INSERT', 'HOME',...
- function keys: 'F1' to 'F12'
- combinations with 'ALT': 'ALT'+'A',...
- combinations with 'CTRL' and 'ALT': 'CTRL'+'ALT'+'SUPR',...
Attention!
'CTRL'+'C' will not be returned by readkey()
, but instead raise a KeyboardInterupt
. If you what to handle it yourself,
use readchar()
. Also note that using the none-blocking version may result in unexpected behaviour for KeyboardInterupt
.
key.py
module
This submodule contains a list of available keys to compare against. You can use it like this:
from readchar import readkey, key
while True:
k = readkey()
if k == key.UP:
# do stuff
if k == key.DOWN:
# do stuff
if k == key.ENTER:
# do stuff
OS Support
This library support both Linux and Windows, but on Windows the key
submodule has fewer keys available.
Currently unsupported, but enabled operating systems:
- macOS
- FreeBSD
Theoretically every Unix based system should work, but they will not be actively tested. It is also required that somebody provides initial test results before the OS is enabled and added to the list. Feel free to open a PR for that.
Thank you!
How to contribute
You can download the code, make some changes with their tests, and open a pull-request.
In order to develop and run the tests, follow these steps:
- Clone the repository.
git clone https://github.com/magmax/python-readchar.git
- Create a virtual environment:
python -m venv .venv
- Enter the virtual environment
source .venv/bin/activate
- Install dependencies
pip install -r requirements.txt
- Install the local version of readchar (in edit mode, so it automatically reflects changes)
pip install -e .
- Run tests
make
Please, Execute the tests before any pull-request. This will avoid invalid builds.
Licence
Copyright (c) 2014-2022 Miguel Angel Garcia (@magmax_en).
Based on previous work on gist getch()-like unbuffered character reading from stdin on both Windows and Unix (Python recipe), started by Danny Yoo as well as gist kbhit.py by Michel Blancard.
Licensed under the MIT licence.