ConnectOnionConnectOnion
DocsTUIKeyboard Input

Keyboard Input

Low-level keyboard input primitives

Quick Start

main.py
1from connectonion.tui import getch, read_key 2 3# Read single character 4ch = getch() # Blocks until keypress 5 6# Read key with arrow handling 7key = read_key() # Returns 'up', 'down', etc.

getch

Read single character without waiting for Enter:

main.py
1from connectonion.tui import getch 2 3ch = getch() 4# Returns: 'a', '1', '\n', etc.

Works on both Unix (termios) and Windows (msvcrt).

read_key

Read key with arrow/escape sequence handling:

main.py
1from connectonion.tui import read_key 2 3key = read_key()

Return Values

InputReturns
Arrow Up'up'
Arrow Down'down'
Arrow Left'left'
Arrow Right'right'
Escape'esc'
Enter'\\n' or '\\r'
Regular charThe character

Example: Navigation Loop

main.py
1from connectonion.tui import read_key 2 3selected = 0 4options = ["Option A", "Option B", "Option C"] 5 6while True: 7 key = read_key() 8 9 if key == 'up': 10 selected = max(0, selected - 1) 11 elif key == 'down': 12 selected = min(len(options) - 1, selected + 1) 13 elif key == '\n': 14 break # Selected 15 elif key == 'esc': 16 selected = -1 17 break # Cancelled

Platform Support

Unix/macOS

Uses termios + tty

Windows

Uses msvcrt