• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python library to read characters and key strokes

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 key-presses:

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

Documentation

There are just two methods:

readchar.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.

readchar.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,...

Note CTRL+C will not be returned by readkey(), but instead raise a KeyboardInterupt. If you want to handle it yourself, use readchar().

readchar.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.

readchar.config class

This static class contains configurations for readchar. It holds constants that are used in other parts of the code as class attributes. You can override/change these to modify its behaviour. Here is a description of the existing attributes:

INTERRUPT_KEYS

List of keys that will result in readkey() raising a KeyboardInterrupt.
Default: [key.CTRL_C]

OS Support

This library actively supports these operating systems:

  • Linux
  • Windows

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

  • macOS
  • FreeBSD / OpenBSD

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 👍. 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

More Repositories

1

python-inquirer

A collection of common interactive command line user interfaces, based on Inquirer.js (https://github.com/SBoudrias/Inquirer.js/)
Python
1,002
star
2

bbb

BuildBot Bootstrap
CSS
9
star
3

colorize

Give some color to your (remote) TTY!!
Python
9
star
4

python-pluginloader

Library to manage python plugins/extensions
Python
6
star
5

pdfcomparator

Compares two PDF files by appearance, not by content. It can be used in the command line, in order to use it inside bigger scripts.
Python
5
star
6

apps

Applications and libraries
Makefile
4
star
7

ansible-opentracing

Observability for Ansible with OpenTracing
Python
3
star
8

pybrowsertest

Browser Tests Framework.
Python
3
star
9

moodle-console

Access to Moodle from command line. Just for UCLM moodle.
HTML
2
star
10

bitbucket-openapi

BitBucket generated library from swagger spec. It is part of Atlassian-OpenApi project: https://github.com/magmax/atlassian-openapi
Python
2
star
11

livedoc

Python
2
star
12

pygrep

Visual grep with profiles and other facilities
Python
2
star
13

diplomacy_advisor

Alerts for webdiplomacy
Python
1
star
14

impinstaller

Multiplatform installer builder
Python
1
star
15

master-java

Ejercicios para el máster de Java que estoy haciendo.
Java
1
star
16

okane

Pequeño proyecto para gestionar la contabilidad doméstica personal.
Ruby
1
star
17

textile2rst

Translator from Textile to ReStructured Text
Python
1
star
18

DjangoBasic

Creación de un sitio básico en Django
Python
1
star
19

python-releaseme

Library and command line app to manage project releases or versions
Python
1
star
20

drupal2blogger

Exporta un site drupal directamente a blogger
Python
1
star
21

itreport

Dump Jira issues to yaml and transform it into reports (PDF, HTML, ...)
Python
1
star
22

angular-example

Example of Angular usage, adding a navigation bar (using Bootstrap) completely functional.
JavaScript
1
star
23

python-orm-mothers

Framework for testing that allows the creation of full randomized objects with history, just based on the ORM model.
Python
1
star
24

osomsite

Make your site awesome by generating just some indexes and rendering it in client.
JavaScript
1
star
25

nikola-theme-octopress

Octopress Theme for Nikola
JavaScript
1
star
26

Charla-Agile

Transparencias para la charla de introducción al desarrollo ágil,
Python
1
star