Skip to main content

Library to easily read single chars and key strokes

Project description

GitHub Repository Latest PyPI version supported Python versions Project licence
Automated testing results Coveralls results Number of PyPI downloads

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

Simply read a character or keystroke:

import readchar

key = readchar.readkey()

React to different kinds of keypresses:

from readchar import readkey, key

while True:
  k = readkey()
  if k == "a":
    # do stuff
  if k == key.DOWN:
    # do stuff
  if k == key.ENTER:
    break

API

There are just two methods:

readchar() -> str

Reads one character from stdin, returning it as a string with length 1. Waits until a character is available.

As only ASCII characters are actually a single character, you usually want to use the next function, that also handles longer keys.

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’,…

key module

This submodule contains a list of available keys to compare against. The constants are defined depending on your operating system, so it should be fully portable. If a key is listed here for your platform, readkey() can read it and you can compare against it.

OS Support

This library actively supports these operating systems:

  • Linux

  • Windows

Some operating systems are enabled, but not actively tested or supported:

  • 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:

  1. Clone the repository.

    git clone https://github.com/magmax/python-readchar.git
  2. Create a virtual environment:

    python -m venv .venv
  3. Enter the virtual environment

    on Linux systems:

    source .venv/bin/activate

    or for Windows systems:

    .venv\Scripts\activate
  4. Install dependencies

    pip install -r requirements.txt
  5. Install the local version of readchar (in edit mode, so it automatically reflects changes)

    pip install -e .
  6. Run tests

    make

    (or run the command used inside the Makefile manually, if you don’t have/want make)

Please, Execute the tests before any pull-request. This will avoid invalid builds.


Copyright (c) 2014-2022 Miguel Angel Garcia

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

readchar-4.0.0.dev0.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

readchar-4.0.0.dev0-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page