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 have an issue problem or found a bug? You have a great new idea or just want to fix a typo? Great :+1:. We are happy to accept your issue or pull request, but first, please read our contribution guidelines. They will also tell you how to write code for this repo and how to properly prepare an issue or a pull request.


Copyright (c) 2014-2022 Miguel Ángel García

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.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

readchar-4.0.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file readchar-4.0.1.tar.gz.

File metadata

  • Download URL: readchar-4.0.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for readchar-4.0.1.tar.gz
Algorithm Hash digest
SHA256 480dd30f836a2e69bfd527d62379788f5f7e0c2615ad25c0eff91b15db6c0f19
MD5 c667883bee9c5e8133408de30e0e9588
BLAKE2b-256 983b3999b5d3c3fbf28792d64be745a22a906c0f3d5033f68dd826bbecbf2bf0

See more details on using hashes here.

File details

Details for the file readchar-4.0.1-py3-none-any.whl.

File metadata

  • Download URL: readchar-4.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for readchar-4.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ab31a7bb626c2780d1c5e1e691fd3c03f00b40afea9f7b5633232c1b98c273ae
MD5 1f6db517c93a7c62bec9ff38583b244c
BLAKE2b-256 09822455fd1737ccf90b42f6edcaa2d7a55f172b38a6d63f416ae6893547ff26

See more details on using hashes here.

Supported by

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