No project description provided
Project description
terminable
Python library for cross-platform terminal input.
- Tested on Windows and macOS, probably works on Linux (not tested yet)
- Does not require root access
- Provides keyboard, mouse, and terminal-resize events
- Simple API that you can plug into your own event loop
- Does not require a full-screen terminal app or separate window
Install
python3 -m pip install terminable
Usage
import terminable
with terminable.capture_input() as terminal_input:
while True:
returned_value = terminal_input.read()
print(f"Input received: {returned_value}\r")
Sample output:
Input received: KeyEvent(Char(a), KeyModifiers.NONE)
Input received: KeyEvent(Char(s), KeyModifiers.NONE)
Input received: KeyEvent(Char(A), KeyModifiers.SHIFT)
Input received: KeyEvent(Char(S), KeyModifiers.SHIFT)
Input received: KeyEvent(Char(F), KeyModifiers.SHIFT)
Input received: KeyEvent(Key.F1, KeyModifiers.NONE)
Input received: KeyEvent(Key.F3, KeyModifiers.NONE)
Input received: KeyEvent(Key.F2, KeyModifiers.NONE)
Input received: KeyEvent(Char(l), KeyModifiers.CONTROL)
Input received: KeyEvent(Char(k), KeyModifiers.CONTROL)
Input received: KeyEvent(Char(p), KeyModifiers.CONTROL)
Input received: MouseEvent(MouseEventKind.MOVED, None, 54, 20, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.MOVED, None, 53, 20, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.MOVED, None, 52, 20, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.MOVED, None, 51, 20, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.MOVED, None, 54, 19, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.SCROLL_DOWN, None, 54, 19, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.SCROLL_DOWN, None, 54, 19, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.SCROLL_DOWN, None, 54, 19, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.SCROLL_DOWN, None, 54, 19, KeyModifiers.NONE)
Input received: ResizeEvent(104, 31)
Input received: ResizeEvent(100, 31)
Input received: ResizeEvent(98, 31)
Input received: ResizeEvent(95, 31)
Types of input
- Keyboard
- Characters
- Arrow keys
- Function keys
- Enter, Esc, Backspace, etc.
- Modifiers:
CONTROL
,SHIFT
,ALT
, etc.
- Mouse
- Move
- Down
- Up
- Drag
- Scroll (scroll wheel, trackpad scroll gestures)
- Resize of terminal window
Implementation
terminable
is a thin Python wrapper around the excellent crossterm
Rust library.
crossterm
does all the heavy lifting. terminable
exposes a small subset of crossterm
functionality to Python using PyO3
.
API
capture_input
terminable
has a single function:
def capture_input() -> InputCapture:
...
InputCapture
The InputCapture
object is a context manager object (intended to be used with Python's with
statement).
When the InputCapture
object is created, the terminal is placed into raw mode, such that:
- Input is not automatically echoed
print
calls do not automatically add a carriage return (\r
)- Control sequences are not automatically processed
When the InputCapture
object is destroyed, the terminal exits raw mode.
read
InputCapture
has a single function:
def read(self) -> KeyEvent | MouseEvent | ResizeEvent:
...
read
blocks until input is received.
Ctrl+C
terminable
raises a KeyboardInterrupt
exception on Ctrl+C
.
Comparison with other libraries
There are other existing libraries for getting terminal input in Python, but each of them have their limitations:
curses
is not supported on Windows and requires a the terminal app to be full screentermios
is not supported on WindowsUniCurses
is a cross-platform implementation ofcurses
, but it is no longer maintained and users have reported issues on Windowspygame
provides comprehensive input functionality, but it requires a separate graphical window and is not well-suited for terminal inputkeyboard
requires root access on some platformspynput
requires root access on some platformstextual
has good cross-platform input handling in a terminal app, but it is tightly coupled with thetextual
application model; you have to lettextual
run your event loop, for example.blessed
has excellent cross-platform keyboard input functionality that is easy to use in your own event loop, butblessed
does not support mouse input.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for terminable-0.1.1-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 087cbf6d82895e6616654bb2afb2c319adf7360c169c9a5cf76fd3433d3818da |
|
MD5 | 569afa1ac3601324efc7562913599b05 |
|
BLAKE2b-256 | 926aa914a5750e9ff0d8ec9943c20eaf023691ea96065088382288afe9a0cb51 |
Hashes for terminable-0.1.1-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9ca495e92cd47ff742a246a6f1dc47be25f2b6e17e4903924ee478e234ef869 |
|
MD5 | 35ae486eb3184c6b8dedb3f8dee92052 |
|
BLAKE2b-256 | 14681892ce4b496d6ee41e0235a50017d6f9320c4c83c09b6dd1ea226c034c24 |
Hashes for terminable-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecb874825309ad74cac7b8c73e105a80cac6fb596c1e5783e00d54c1dd2f63b8 |
|
MD5 | 896c6211dac304358bbec36a9136949b |
|
BLAKE2b-256 | 44a0c1f4006f99bab454ea63beea94b9bf81972c47e50ff6dbc46956338e8e51 |