|
| 1 | +[](https://github.com/magmax/python-readchar) |
| 2 | +[](https://pypi.python.org/pypi/readchar) |
| 3 | +[](https://pypi.python.org/pypi/readchar) |
| 4 | +[](LICENCE) \ |
| 5 | +[](https://github.com/magmax/python-readchar/actions/workflows/run-tests.yaml?query=branch%3Amaster) |
| 6 | +[](https://coveralls.io/github/magmax/python-readchar?branch=master) |
| 7 | +[](https://pypi.python.org/pypi/readchar) |
| 8 | + |
| 9 | +# python-readchar |
| 10 | + |
| 11 | +Library to easily read single chars and keystrokes. |
| 12 | + |
| 13 | +Born as a [python-inquirer](https://github.com/magmax/python-inquirer) requirement. |
| 14 | + |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +simply install it via `pip`: |
| 19 | + |
| 20 | +```bash |
| 21 | +pip install readchar |
| 22 | +``` |
| 23 | + |
| 24 | +Or download the source code from [PyPi](https://pypi.python.org/pypi/readchar). |
| 25 | + |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +Simply read a character or keystroke: |
| 30 | + |
| 31 | +```python |
| 32 | +import readchar |
| 33 | + |
| 34 | +key = readchar.readkey() |
| 35 | +``` |
| 36 | + |
| 37 | +React to different kinds of keypresses: |
| 38 | + |
| 39 | +```python |
| 40 | +from readchar import readkey, key |
| 41 | + |
| 42 | +while True: |
| 43 | + k = readkey() |
| 44 | + if k == "a": |
| 45 | + # do stuff |
| 46 | + if k == key.DOWN: |
| 47 | + # do stuff |
| 48 | + if k == key.ENTER: |
| 49 | + break |
| 50 | +``` |
| 51 | + |
| 52 | + |
| 53 | +## API |
| 54 | + |
| 55 | +There are just two methods: |
| 56 | + |
| 57 | +### `readchar() -> str` |
| 58 | + |
| 59 | +Reads one character from `stdin`, returning it as a string with length 1. Waits until a character is available. |
| 60 | + |
| 61 | +As only ASCII characters are actually a single character, you usually want to use the next function, that also handles longer keys. |
| 62 | + |
| 63 | +### `readkey() -> str` |
| 64 | + |
| 65 | +Reads the next keystroke from `stdin`, returning it as a string. Waits |
| 66 | +until a keystroke is available. |
| 67 | + |
| 68 | +A keystroke can be: |
| 69 | + |
| 70 | +- single characters as returned by `readchar()`. These include: |
| 71 | + - character for normal keys: 'a', 'Z', '9',... |
| 72 | + - special characters like 'ENTER', 'BACKSPACE', 'TAB',... |
| 73 | + - combinations with 'CTRL': 'CTRL'+'A',... |
| 74 | +- keys that are made up of multiple characters: |
| 75 | + - characters for cursors/arrows: 🡩, 🡪, 🡫, 🡨 |
| 76 | + - navigation keys: 'INSERT', 'HOME',... |
| 77 | + - function keys: 'F1' to 'F12' |
| 78 | + - combinations with 'ALT': 'ALT'+'A',... |
| 79 | + - combinations with 'CTRL' and 'ALT': 'CTRL'+'ALT'+'SUPR',... |
| 80 | + |
| 81 | +> **Note** |
| 82 | +> 'CTRL'+'C' will not be returned by `readkey()`, but instead raise a `KeyboardInterupt`. If you what to handle it yourself, use `readchar()`. |
| 83 | +
|
| 84 | +### `readchar.key` module |
| 85 | + |
| 86 | +This submodule contains a list of available keys to compare against. The constants are defined depending on your operating system, so it should be |
| 87 | +fully portable. If a key is listed here for your platform, `readkey()` can read it, and you can compare against it. |
| 88 | + |
| 89 | + |
| 90 | +## OS Support |
| 91 | + |
| 92 | +This library actively supports these operating systems: |
| 93 | + |
| 94 | +- Linux |
| 95 | +- Windows |
| 96 | + |
| 97 | +Some operating systems are enabled, but not actively tested or supported: |
| 98 | + |
| 99 | +- macOS |
| 100 | +- FreeBSD |
| 101 | + |
| 102 | +Theoretically every Unix based system should work, but they will not be actively tested. It is also required that somebody provides initial test |
| 103 | +results before the OS is enabled and added to the list. Feel free to open a PR for that. |
| 104 | + |
| 105 | +Thank you! |
| 106 | + |
| 107 | + |
| 108 | +## How to contribute |
| 109 | + |
| 110 | +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 |
| 111 | +request, but first, please read our [contribution guidelines](https://github.com/magmax/python-readchar/blob/master/CONTRIBUTING.md). They will also |
| 112 | +tell you how to write code for this repo and how to properly prepare an issue or a pull request. |
| 113 | + |
| 114 | +----- |
| 115 | + |
| 116 | +*Copyright (c) 2014-2022 Miguel Ángel García* |
0 commit comments