input
File providing the getch() function to easily read character inputs.
Credits: - Original getch implementation: Danny Yoo (https://code.activestate.com/recipes/134892) - Modern additions & idea: kcsaff (https://github.com/kcsaff/getkey)
Keys
Class for easy access to key-codes.
The keys for CTRL_{ascii_letter}-s can be generated with the following code:
for i, letter in enumerate(ascii_lowercase):
key = f"CTRL_{letter.upper()}"
code = chr(i+1).encode('unicode_escape').decode('utf-8')
print(key, code)
Source code in pytermgui/input.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
__getattr__(attr)
Gets attr from self._keys.
Source code in pytermgui/input.py
263 264 265 266 267 268 269 |
|
__init__(platform_keys, platform)
Initialize Keys object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
platform_keys |
dict[str, str]
|
A dictionary of platform-specific keys. |
required |
platform |
str
|
The platform the program is running on. |
required |
Source code in pytermgui/input.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
get_name(key, default=None)
Gets canonical name of a key code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The key to get the name of. |
required |
default |
Optional[str]
|
The return value to substitute if no canonical name could be found. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Optional[str]
|
The canonical name if one can be found, default otherwise. |
Source code in pytermgui/input.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
items()
Returns items() of self._keys.
Source code in pytermgui/input.py
299 300 301 302 |
|
keys()
Returns keys() of self._keys.
Source code in pytermgui/input.py
294 295 296 297 |
|
values()
Returns values() of self._keys.
Source code in pytermgui/input.py
289 290 291 292 |
|
getch(printable=False, interrupts=True)
Wrapper to call the platform-appropriate character getter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
printable |
bool
|
When set, printable versions of the input are returned. |
False
|
interrupts |
bool
|
If not set, |
True
|
Source code in pytermgui/input.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
getch_timeout(duration, default='', printable=False, interrupts=True)
Calls getch
, returns default
if timeout passes before getting input.
No timeout is applied on Windows systems, as there is no support for SIGALRM
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
duration |
float
|
How long the call should wait for input. |
required |
default |
str
|
The value to return if timeout occured. |
''
|
Source code in pytermgui/input.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
|
timeout(duration)
Allows context to run for a certain amount of time, quits it once it's up.
Note that this should never be run on Windows, as the required signals are not present. Whenever this function is run, there should be a preliminary OS check, to avoid running into issues on unsupported machines.
Source code in pytermgui/input.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|