|
1 | | -See it at: |
| 1 | +| |GitHub badge| |PyPi badge| |python versions badge| |licence badge| |
| 2 | +| |test status| |coverage status| |pip downloads badge| |
2 | 3 |
|
3 | | -- `pypi`_ |
4 | | -- `GitHub`_ |
| 4 | +python-readchar |
| 5 | +*************** |
5 | 6 |
|
6 | | -============== =============== ========= ============ |
7 | | -VERSION DOWNLOADS TESTS COVERAGE |
8 | | -============== =============== ========= ============ |
9 | | -|pip version| |pip downloads| |travis| |coveralls| |
10 | | -============== =============== ========= ============ |
11 | | - |
12 | | -Library to easily read single chars and key strokes. |
13 | | - |
14 | | -Goal and Philosophy |
15 | | -=================== |
| 7 | +Library to easily read single chars and keystrokes. |
16 | 8 |
|
17 | 9 | Born as a `python-inquirer`_ requirement. |
18 | 10 |
|
19 | | -The idea is to have a portable way to read **single** characters and **key-strokes**. |
20 | | - |
21 | | - |
22 | | -Documentation |
23 | | -============= |
24 | 11 |
|
25 | 12 | Installation |
26 | | ------------- |
| 13 | +============ |
| 14 | + |
| 15 | +simply install it via :code:`pip`: |
27 | 16 |
|
28 | | -:: |
| 17 | +.. code:: bash |
29 | 18 |
|
30 | 19 | pip install readchar |
31 | 20 |
|
32 | | -The :code:`readchar` library works with python 3.6, 3.7, 3.8, 3.9 and 3.10. |
| 21 | +Or download the source code from PyPi_. |
| 22 | + |
33 | 23 |
|
34 | 24 | Usage |
35 | | ------ |
| 25 | +===== |
36 | 26 |
|
37 | | -Usage example: |
| 27 | +Simply read a character or keystroke: |
38 | 28 |
|
39 | 29 | .. code:: python |
40 | 30 |
|
41 | 31 | import readchar |
42 | 32 |
|
43 | | - c = readchar.readchar() |
44 | 33 | key = readchar.readkey() |
45 | 34 |
|
| 35 | +React to different kinds of keypresses: |
| 36 | + |
| 37 | +.. code:: python |
| 38 | +
|
| 39 | + from readchar import readkey, key |
| 40 | +
|
| 41 | + while True: |
| 42 | + k = readkey() |
| 43 | + if k == "a": |
| 44 | + # do stuff |
| 45 | + if k == key.DOWN: |
| 46 | + # do stuff |
| 47 | + if k == key.ENTER: |
| 48 | + break |
| 49 | +
|
| 50 | +
|
46 | 51 | API |
47 | | ----- |
| 52 | +=== |
48 | 53 |
|
49 | 54 | There are just two methods: |
50 | 55 |
|
51 | | -:code:`readchar()` |
52 | | -////////////////// |
| 56 | +:code:`readchar() -> str` |
| 57 | +------------------------- |
| 58 | + |
| 59 | +Reads one character from :code:`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 | + |
| 64 | +:code:`readkey() -> str` |
| 65 | +------------------------ |
| 66 | + |
| 67 | +Reads the next keystroke from :code:`stdin`, returning it as a string. Waits until a keystroke is available. |
| 68 | + |
| 69 | +A keystroke can be: |
53 | 70 |
|
54 | | -Reads the next char from :code:`stdin`, returning it as a string with length 1. |
| 71 | +- single characters as returned by :code:`readchar()`. These include: |
55 | 72 |
|
| 73 | + - character for normal keys: 'a', 'Z', '9',... |
| 74 | + - special characters like 'ENTER', 'BACKSPACE', 'TAB',... |
| 75 | + - combinations with 'CTRL': 'CTRL'+'A',... |
56 | 76 |
|
57 | | -:code:`readkey()` |
58 | | -///////////////// |
| 77 | +- keys that are made up of multiple characters: |
59 | 78 |
|
60 | | -Reads the next key-stroke from :code:`stdin`, returning it as a string. |
| 79 | + - characters for cursors/arrows: 🡩, 🡪, 🡫, 🡨 |
| 80 | + - navigation keys: 'INSERT', 'HOME',... |
| 81 | + - function keys: 'F1' to 'F12' |
| 82 | + - combinations with 'ALT': 'ALT'+'A',... |
| 83 | + - combinations with 'CTRL' and 'ALT': 'CTRL'+'ALT'+'SUPR',... |
61 | 84 |
|
62 | | -A key-stroke can have: |
| 85 | +.. attention:: |
63 | 86 |
|
64 | | -- 1 character for normal keys: 'a', 'z', '9'... |
65 | | -- 2 characters for combinations with ALT: ALT+A, ... |
66 | | -- 3 characters for cursors: ->, <-, ... |
67 | | -- 4 characters for combinations with CTRL and ALT: CTRL+ALT+SUPR, ... |
| 87 | + 'CTRL'+'C' will not be returned by :code:`readkey()`, but instead raise a :code:`KeyboardInterupt`. If you what to handle it yourself, |
| 88 | + use :code:`readchar()`. |
68 | 89 |
|
69 | | -There is a list of previously captured chars with their names in :code:`readchar.key`, in order to be used in comparisons and so on. This list is not enough tested and it can have mistakes, so use it carefully. Please, report them if found. |
| 90 | + |
| 91 | +:code:`key` module |
| 92 | +--------------------- |
| 93 | + |
| 94 | +This submodule contains a list of available keys to compare against. The constants are defined depending on your operating system, so it should be |
| 95 | +fully portable. If a key is listed here for your platform, :code:`readkey()` can read it and you can compare against it. |
70 | 96 |
|
71 | 97 |
|
72 | 98 | OS Support |
73 | | ----------- |
| 99 | +========== |
| 100 | + |
| 101 | +This library actively supports these operating systems: |
| 102 | + |
| 103 | +- Linux |
| 104 | +- Windows |
| 105 | + |
| 106 | +Some operating systems are enabled, but not actively tested or supported: |
74 | 107 |
|
75 | | -This library supports GNU/Linux and Windows. Please, if you can try it in another OS and find a bug, put an issue or send the pull-request. |
| 108 | +- macOS |
| 109 | +- FreeBSD |
| 110 | + |
| 111 | +Theoretically every Unix based system should work, but they will not be actively tested. It is also required that somebody provides initial test |
| 112 | +results before the OS is enabled and added to the list. Feel free to open a PR for that. |
76 | 113 |
|
77 | 114 | Thank you! |
78 | 115 |
|
| 116 | + |
79 | 117 | How to contribute |
80 | 118 | ================= |
81 | 119 |
|
82 | | -You can download the code, make some changes with their tests, and make a pull-request. |
| 120 | +You can download the code, make some changes with their tests, and open a pull-request. |
83 | 121 |
|
84 | | -In order to develop or running the tests, you can do: |
| 122 | +In order to develop and run the tests, follow these steps: |
85 | 123 |
|
86 | | -1. Clone the repository. |
| 124 | +1. Clone the repository. |
87 | 125 |
|
88 | | -.. code:: bash |
| 126 | + .. code:: bash |
89 | 127 |
|
90 | | - git clone https://github.com/magmax/python-readchar.git |
| 128 | + git clone https://github.com/magmax/python-readchar.git |
91 | 129 |
|
92 | | -2. Create a virtual environment: |
| 130 | +2. Create a virtual environment: |
93 | 131 |
|
94 | | -.. code:: bash |
| 132 | + .. code:: bash |
95 | 133 |
|
96 | | - python -m venv .venv |
| 134 | + python -m venv .venv |
97 | 135 |
|
98 | | -3. Enter in the virtual environment |
| 136 | +3. Enter the virtual environment |
99 | 137 |
|
100 | | -.. code:: bash |
| 138 | + on Linux systems: |
101 | 139 |
|
102 | | - source .venv/bin/activate |
| 140 | + .. code:: bash |
103 | 141 |
|
104 | | -4. Install dependencies |
| 142 | + source .venv/bin/activate |
105 | 143 |
|
106 | | -.. code:: bash |
| 144 | + or for Windows systems: |
107 | 145 |
|
108 | | - pip install -r requirements.txt |
| 146 | + .. code:: bash |
109 | 147 |
|
110 | | -5. Install the local version of readchar (in edit mode so it automatically reflects changes) |
| 148 | + .venv\Scripts\activate |
111 | 149 |
|
112 | | -.. code:: bash |
| 150 | +4. Install dependencies |
113 | 151 |
|
114 | | - pip install -e . |
| 152 | + .. code:: bash |
115 | 153 |
|
116 | | -6. Run tests |
| 154 | + pip install -r requirements.txt |
117 | 155 |
|
118 | | -.. code:: bash |
| 156 | +5. Install the local version of readchar (in edit mode, so it automatically reflects changes) |
119 | 157 |
|
120 | | - make |
| 158 | + .. code:: bash |
121 | 159 |
|
| 160 | + pip install -e . |
122 | 161 |
|
123 | | -Please, **Execute the tests before any pull-request**. This will avoid invalid builds. |
| 162 | +6. Run tests |
124 | 163 |
|
| 164 | + .. code:: bash |
125 | 165 |
|
126 | | -Licence |
127 | | -======= |
| 166 | + make |
128 | 167 |
|
129 | | -Copyright (c) 2014-2021 Miguel Angel Garcia (`@magmax_en`_). |
| 168 | + (or run the command used inside the Makefile manually, if you don't have/want :code:`make`) |
130 | 169 |
|
131 | | -Based on previous work on gist `getch()-like unbuffered character reading from stdin on both Windows and Unix (Python recipe)`_, started by `Danny Yoo`_. |
132 | 170 |
|
133 | | -Licensed under `the MIT licence`_. |
| 171 | +Please, **Execute the tests before any pull-request**. This will avoid invalid builds. |
134 | 172 |
|
135 | 173 |
|
136 | | -.. |travis| image:: https://travis-ci.org/magmax/python-readchar.png |
137 | | - :target: `Travis`_ |
138 | | - :alt: Travis results |
| 174 | +------ |
139 | 175 |
|
140 | | -.. |coveralls| image:: https://coveralls.io/repos/magmax/python-readchar/badge.png |
141 | | - :target: `Coveralls`_ |
142 | | - :alt: Coveralls results_ |
| 176 | +*Copyright (c) 2014-2022 Miguel Angel Garcia* |
143 | 177 |
|
144 | | -.. |pip version| image:: https://img.shields.io/pypi/v/readchar.svg |
145 | | - :target: https://pypi.python.org/pypi/readchar |
146 | | - :alt: Latest PyPI version |
147 | 178 |
|
148 | | -.. |pip downloads| image:: https://img.shields.io/pypi/dm/readchar.svg |
149 | | - :target: https://pypi.python.org/pypi/readchar |
| 179 | +.. |GitHub badge| image:: https://badges.aleen42.com/src/github.svg |
| 180 | + :target: GitHub_ |
| 181 | + :alt: GitHub Repository |
| 182 | +.. |PyPi badge| image:: https://img.shields.io/pypi/v/readchar.svg |
| 183 | + :target: PyPi_ |
| 184 | + :alt: Latest PyPI version |
| 185 | +.. |Python versions badge| image:: https://img.shields.io/pypi/pyversions/readchar |
| 186 | + :target: PyPi_ |
| 187 | + :alt: supported Python versions |
| 188 | +.. |licence badge| image:: https://img.shields.io/pypi/l/readchar?color=blue |
| 189 | + :target: licence_ |
| 190 | + :alt: Project licence |
| 191 | +.. |test status| image:: https://github.com/magmax/python-readchar/actions/workflows/run-tests.yml/badge.svg?branch=master |
| 192 | + :target: github.com/magmax/python-readchar/actions/workflows/run-tests.yml?query=branch%3Amaster |
| 193 | + :alt: Automated testing results |
| 194 | +.. |coverage status| image:: https://coveralls.io/repos/github/magmax/python-readchar/badge.svg?branch=master |
| 195 | + :target: https://coveralls.io/github/magmax/python-readchar?branch=master |
| 196 | + :alt: Coveralls results |
| 197 | +.. |pip downloads badge| image:: https://img.shields.io/pypi/dd/readchar.svg |
| 198 | + :target: PyPi_ |
150 | 199 | :alt: Number of PyPI downloads |
151 | 200 |
|
152 | | -.. _pypi: https://pypi.python.org/pypi/readchar |
153 | 201 | .. _GitHub: https://github.com/magmax/python-readchar |
| 202 | +.. _PyPi: https://pypi.python.org/pypi/readchar |
| 203 | +.. _licence: LICENCE |
154 | 204 | .. _python-inquirer: https://github.com/magmax/python-inquirer |
155 | | -.. _Travis: https://travis-ci.org/magmax/python-readchar |
156 | | -.. _Coveralls: https://coveralls.io/r/magmax/python-readchar |
157 | | -.. _@magmax_en: https://twitter.com/magmax_en |
158 | | - |
159 | | -.. _the MIT licence: LICENCE |
160 | | -.. _getch()-like unbuffered character reading from stdin on both Windows and Unix (Python recipe): http://code.activestate.com/recipes/134892/ |
161 | | -.. _Danny Yoo: http://code.activestate.com/recipes/users/98032/ |
|
0 commit comments