readchar


Namereadchar JSON
Version 4.0.4 PyPI version JSON
download
home_pagehttps://github.com/magmax/python-readchar
SummaryLibrary to easily read single chars and key strokes
upload_time2023-03-15 11:43:03
maintainer
docs_urlNone
authorMiguel Ángel García
requires_python>=3.7
licenseMIT
keywords characters keystrokes stdin command line
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![GitHub Repository](https://img.shields.io/badge/-GitHub-%230D0D0D?logo=github&labelColor=gray)](https://github.com/magmax/python-readchar)
[![Latest PyPi version](https://img.shields.io/pypi/v/readchar.svg)](https://pypi.python.org/pypi/readchar)
[![supported Python versions](https://img.shields.io/pypi/pyversions/readchar)](https://pypi.python.org/pypi/readchar)
[![Project licence](https://img.shields.io/pypi/l/readchar?color=blue)](LICENCE) <br>
[![Automated testing results](https://img.shields.io/github/actions/workflow/status/magmax/python-readchar/run-tests.yaml?branch=master)](https://github.com/magmax/python-readchar/actions/workflows/run-tests.yaml?query=branch%3Amaster)
[![Coveralls results](https://coveralls.io/repos/github/magmax/python-readchar/badge.svg?branch=master)](https://coveralls.io/github/magmax/python-readchar?branch=master)
[![Number of PyPi downloads](https://img.shields.io/pypi/dd/readchar.svg)](https://pypi.python.org/pypi/readchar)

# python-readchar

Library to easily read single chars and keystrokes.

Born as a [python-inquirer](https://github.com/magmax/python-inquirer) requirement.

## Installation

simply install it via `pip`:

```bash
pip install readchar
```

Or download the source code from [PyPi](https://pypi.python.org/pypi/readchar).

## Usage

Simply read a character or keystroke:

```python
import readchar

key = readchar.readkey()
```

React to different kinds of key-presses:

```python
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: <kbd>a</kbd>, <kbd>Z</kbd>, <kbd>9</kbd>,...
  - special characters like <kbd>ENTER</kbd>, <kbd>BACKSPACE</kbd>, <kbd>TAB</kbd>,...
  - combinations with <kbd>CTRL</kbd>: <kbd>CTRL</kbd>+<kbd>A</kbd>,...
- keys that are made up of multiple characters:
  - characters for cursors/arrows: <kbd>🡩</kbd>, <kbd>🡪</kbd>, <kbd>🡫</kbd>,
    <kbd>🡨</kbd>
  - navigation keys: <kbd>INSERT</kbd>, <kbd>HOME</kbd>,...
  - function keys: <kbd>F1</kbd> to <kbd>F12</kbd>
  - combinations with <kbd>ALT</kbd>: <kbd>ALT</kbd>+<kbd>A</kbd>,...
  - combinations with <kbd>CTRL</kbd> and <kbd>ALT</kbd>:
    <kbd>CTRL</kbd>+<kbd>ALT</kbd>+<kbd>SUPR</kbd>,...

> **Note** <kbd>CTRL</kbd>+<kbd>C</kbd> will not be returned by `readkey()`, but instead
> raise a `KeyboardInterupt`. If you what 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:

<dl>
<dt><code>INTERRUPT_KEYS</code></dt>
<dd>

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

</dd>
</dl>

## 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](https://github.com/magmax/python-readchar/blob/master/CONTRIBUTING.md).
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*

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/magmax/python-readchar",
    "name": "readchar",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "characters,keystrokes,stdin,command line",
    "author": "Miguel \u00c1ngel Garc\u00eda",
    "author_email": "miguelangel.garcia@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/01/58/7f0ebec28fd6fe67e29e9f70c76d667d2f01e202758fbb620d7caed2be9c/readchar-4.0.4.tar.gz",
    "platform": null,
    "description": "[![GitHub Repository](https://img.shields.io/badge/-GitHub-%230D0D0D?logo=github&labelColor=gray)](https://github.com/magmax/python-readchar)\n[![Latest PyPi version](https://img.shields.io/pypi/v/readchar.svg)](https://pypi.python.org/pypi/readchar)\n[![supported Python versions](https://img.shields.io/pypi/pyversions/readchar)](https://pypi.python.org/pypi/readchar)\n[![Project licence](https://img.shields.io/pypi/l/readchar?color=blue)](LICENCE) <br>\n[![Automated testing results](https://img.shields.io/github/actions/workflow/status/magmax/python-readchar/run-tests.yaml?branch=master)](https://github.com/magmax/python-readchar/actions/workflows/run-tests.yaml?query=branch%3Amaster)\n[![Coveralls results](https://coveralls.io/repos/github/magmax/python-readchar/badge.svg?branch=master)](https://coveralls.io/github/magmax/python-readchar?branch=master)\n[![Number of PyPi downloads](https://img.shields.io/pypi/dd/readchar.svg)](https://pypi.python.org/pypi/readchar)\n\n# python-readchar\n\nLibrary to easily read single chars and keystrokes.\n\nBorn as a [python-inquirer](https://github.com/magmax/python-inquirer) requirement.\n\n## Installation\n\nsimply install it via `pip`:\n\n```bash\npip install readchar\n```\n\nOr download the source code from [PyPi](https://pypi.python.org/pypi/readchar).\n\n## Usage\n\nSimply read a character or keystroke:\n\n```python\nimport readchar\n\nkey = readchar.readkey()\n```\n\nReact to different kinds of key-presses:\n\n```python\nfrom readchar import readkey, key\n\nwhile True:\n  k = readkey()\n  if k == \"a\":\n    # do stuff\n  if k == key.DOWN:\n    # do stuff\n  if k == key.ENTER:\n    break\n```\n\n## Documentation\n\nThere are just two methods:\n\n### `readchar.readchar() -> str`\n\nReads one character from `stdin`, returning it as a string with length 1. Waits until a\ncharacter is available.\n\nAs only ASCII characters are actually a single character, you usually want to use the\nnext function, that also handles longer keys.\n\n### `readchar.readkey() -> str`\n\nReads the next keystroke from `stdin`, returning it as a string. Waits until a keystroke\nis available.\n\nA keystroke can be:\n\n- single characters as returned by `readchar()`. These include:\n  - character for normal keys: <kbd>a</kbd>, <kbd>Z</kbd>, <kbd>9</kbd>,...\n  - special characters like <kbd>ENTER</kbd>, <kbd>BACKSPACE</kbd>, <kbd>TAB</kbd>,...\n  - combinations with <kbd>CTRL</kbd>: <kbd>CTRL</kbd>+<kbd>A</kbd>,...\n- keys that are made up of multiple characters:\n  - characters for cursors/arrows: <kbd>\ud83e\udc69</kbd>, <kbd>\ud83e\udc6a</kbd>, <kbd>\ud83e\udc6b</kbd>,\n    <kbd>\ud83e\udc68</kbd>\n  - navigation keys: <kbd>INSERT</kbd>, <kbd>HOME</kbd>,...\n  - function keys: <kbd>F1</kbd> to <kbd>F12</kbd>\n  - combinations with <kbd>ALT</kbd>: <kbd>ALT</kbd>+<kbd>A</kbd>,...\n  - combinations with <kbd>CTRL</kbd> and <kbd>ALT</kbd>:\n    <kbd>CTRL</kbd>+<kbd>ALT</kbd>+<kbd>SUPR</kbd>,...\n\n> **Note** <kbd>CTRL</kbd>+<kbd>C</kbd> will not be returned by `readkey()`, but instead\n> raise a `KeyboardInterupt`. If you what to handle it yourself, use `readchar()`.\n\n### `readchar.key` module\n\nThis submodule contains a list of available keys to compare against. The constants are\ndefined depending on your operating system, so it should be fully portable. If a key is\nlisted here for your platform, `readkey()` can read it, and you can compare against it.\n\n### `readchar.config` class\n\nThis static class contains configurations for `readchar`. It holds constants that are\nused in other parts of the code as class attributes. You can override/change these to\nmodify its behaviour. Here is a description of the existing attributes:\n\n<dl>\n<dt><code>INTERRUPT_KEYS</code></dt>\n<dd>\n\nList of keys that will result in `readkey()` raising a `KeyboardInterrupt`. <br>\n*Default:* `[key.CTRL_C]`\n\n</dd>\n</dl>\n\n## OS Support\n\nThis library actively supports these operating systems:\n\n- Linux\n- Windows\n\nSome operating systems are enabled, but not actively tested or supported:\n\n- macOS\n- FreeBSD\n\nTheoretically every Unix based system should work, but they will not be actively tested.\nIt is also required that somebody provides initial test results before the OS is enabled\nand added to the list. Feel free to open a PR for that.\n\nThank you!\n\n## How to contribute\n\nYou have an issue problem or found a bug? You have a great new idea or just want to fix\na typo? Great :+1:. We are happy to accept your issue or pull request, but first, please\nread our\n[contribution guidelines](https://github.com/magmax/python-readchar/blob/master/CONTRIBUTING.md).\nThey will also tell you how to write code for this repo and how to properly prepare an\nissue or a pull request.\n\n______________________________________________________________________\n\n*Copyright (c) 2014-2022 Miguel \u00c1ngel Garc\u00eda*\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Library to easily read single chars and key strokes",
    "version": "4.0.4",
    "split_keywords": [
        "characters",
        "keystrokes",
        "stdin",
        "command line"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "acf7c9697f8f9105659bb0cf74d13af4ff58e1370149e77f01ba0118e3978f15",
                "md5": "c00735c98a258e4e24bc559c3fcd6919",
                "sha256": "274e08aebb64542362ebf0b06cfa8e7ba7595dc419b812c19d6bc17c2d4fa4bb"
            },
            "downloads": -1,
            "filename": "readchar-4.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c00735c98a258e4e24bc559c3fcd6919",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 8501,
            "upload_time": "2023-03-15T11:43:01",
            "upload_time_iso_8601": "2023-03-15T11:43:01.857932Z",
            "url": "https://files.pythonhosted.org/packages/ac/f7/c9697f8f9105659bb0cf74d13af4ff58e1370149e77f01ba0118e3978f15/readchar-4.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "01587f0ebec28fd6fe67e29e9f70c76d667d2f01e202758fbb620d7caed2be9c",
                "md5": "b31b18dc5be37a3c4a16d8f6b0b31e55",
                "sha256": "79b1d27eb9160568102219c872fef8d13bf7f16094c09ee287f620f6f2a7c031"
            },
            "downloads": -1,
            "filename": "readchar-4.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "b31b18dc5be37a3c4a16d8f6b0b31e55",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 9075,
            "upload_time": "2023-03-15T11:43:03",
            "upload_time_iso_8601": "2023-03-15T11:43:03.584307Z",
            "url": "https://files.pythonhosted.org/packages/01/58/7f0ebec28fd6fe67e29e9f70c76d667d2f01e202758fbb620d7caed2be9c/readchar-4.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-15 11:43:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "magmax",
    "github_project": "python-readchar",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "readchar"
}
        
Elapsed time: 0.04377s