Module livekit.agents.cli.readchar

Functions

def readchar() ‑> str
Expand source code
def _posix_readchar() -> str:
    """Read a single character from standard input on POSIX systems.

    This function blocks until a character is available.  It uses
    ``termios`` to disable canonical input processing and echo so
    characters are returned immediately and without being echoed to
    the terminal.  The implementation closely follows the upstream
    ``_posix_read.readchar`` function.
    """
    import termios
    import tty

    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    term = termios.tcgetattr(fd)
    try:
        term[3] &= ~(termios.ICANON | termios.ECHO)
        term[3] |= termios.ISIG
        termios.tcsetattr(fd, termios.TCSAFLUSH, term)

        ch = sys.stdin.read(1)
    finally:
        try:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        except Exception:
            try:
                tty.setcbreak(fd)
                cur = termios.tcgetattr(fd)
                cur[3] |= termios.ICANON | termios.ECHO | termios.ISIG
                termios.tcsetattr(fd, termios.TCSADRAIN, cur)
            except Exception:
                pass
    return ch

Read a single character from standard input on POSIX systems.

This function blocks until a character is available. It uses termios to disable canonical input processing and echo so characters are returned immediately and without being echoed to the terminal. The implementation closely follows the upstream _posix_read.readchar function.

def readkey() ‑> str
Expand source code
def _posix_readkey() -> str:
    """Read the next keypress on POSIX systems.

    If a multi‑byte escape sequence is encountered (for example, an arrow
    key or function key), the entire sequence is read and returned.
    ``KeyboardInterrupt`` is raised when a key listed in
    :data:`config.INTERRUPT_KEYS` is pressed.
    """
    c1 = _posix_readchar()

    if c1 in INTERRUPT_KEYS:
        raise KeyboardInterrupt

    # Not an escape sequence; return immediately
    if c1 != "\x1b":
        return c1

    # Escape sequence – read second byte
    c2 = _posix_readchar()
    if c2 not in "\x4f\x5b":
        return c1 + c2

    # Third byte distinguishes between simple arrows and multi‑byte
    c3 = _posix_readchar()
    if c3 not in "\x31\x32\x33\x35\x36":
        return c1 + c2 + c3

    # Fourth byte for multi‑byte function/navigation keys
    c4 = _posix_readchar()
    if c4 not in "\x30\x31\x33\x34\x35\x37\x38\x39":
        return c1 + c2 + c3 + c4

    # Fifth byte for the remainder of the sequence
    c5 = _posix_readchar()
    return c1 + c2 + c3 + c4 + c5

Read the next keypress on POSIX systems.

If a multi‑byte escape sequence is encountered (for example, an arrow key or function key), the entire sequence is read and returned. KeyboardInterrupt is raised when a key listed in :data:config.INTERRUPT_KEYS is pressed.

Classes

class key
Expand source code
class _PosixKey(_BaseKey):
    """Namespace of key codes specific to POSIX platforms (Linux, macOS, BSD).

    These values mirror those defined in the upstream ``_posix_key.py``
    module.  All attributes from :class:`_BaseKey` are inherited.
    """

    # Common additional control character
    BACKSPACE: ClassVar[str] = "\x7f"

    # Cursor movement (escape sequences)
    UP: ClassVar[str] = "\x1b\x5b\x41"
    DOWN: ClassVar[str] = "\x1b\x5b\x42"
    LEFT: ClassVar[str] = "\x1b\x5b\x44"
    RIGHT: ClassVar[str] = "\x1b\x5b\x43"

    # Navigation keys
    INSERT: ClassVar[str] = "\x1b\x5b\x32\x7e"
    SUPR: ClassVar[str] = "\x1b\x5b\x33\x7e"
    HOME: ClassVar[str] = "\x1b\x5b\x48"
    END: ClassVar[str] = "\x1b\x5b\x46"
    PAGE_UP: ClassVar[str] = "\x1b\x5b\x35\x7e"
    PAGE_DOWN: ClassVar[str] = "\x1b\x5b\x36\x7e"

    # Function keys
    F1: ClassVar[str] = "\x1b\x4f\x50"
    F2: ClassVar[str] = "\x1b\x4f\x51"
    F3: ClassVar[str] = "\x1b\x4f\x52"
    F4: ClassVar[str] = "\x1b\x4f\x53"
    F5: ClassVar[str] = "\x1b\x5b\x31\x35\x7e"
    F6: ClassVar[str] = "\x1b\x5b\x31\x37\x7e"
    F7: ClassVar[str] = "\x1b\x5b\x31\x38\x7e"
    F8: ClassVar[str] = "\x1b\x5b\x31\x39\x7e"
    F9: ClassVar[str] = "\x1b\x5b\x32\x30\x7e"
    F10: ClassVar[str] = "\x1b\x5b\x32\x31\x7e"
    F11: ClassVar[str] = "\x1b\x5b\x32\x33\x7e"
    F12: ClassVar[str] = "\x1b\x5b\x32\x34\x7e"

    # Shift/other combinations
    SHIFT_TAB: ClassVar[str] = "\x1b\x5b\x5a"
    CTRL_ALT_SUPR: ClassVar[str] = "\x1b\x5b\x33\x5e"

    # ALT combinations
    ALT_A: ClassVar[str] = "\x1b\x61"

    # CTRL+ALT combinations
    CTRL_ALT_A: ClassVar[str] = "\x1b\x01"

    # Aliases to improve readability
    ENTER: ClassVar[str] = _BaseKey.LF
    DELETE: ClassVar[str] = SUPR

Namespace of key codes specific to POSIX platforms (Linux, macOS, BSD).

These values mirror those defined in the upstream _posix_key.py module. All attributes from :class:_BaseKey are inherited.

Ancestors

  • livekit.agents.cli.readchar._BaseKey

Class variables

var ALT_A : ClassVar[str]
var BACKSPACE : ClassVar[str]
var CTRL_ALT_A : ClassVar[str]
var CTRL_ALT_SUPR : ClassVar[str]
var DELETE : ClassVar[str]
var DOWN : ClassVar[str]
var END : ClassVar[str]
var ENTER : ClassVar[str]
var F1 : ClassVar[str]
var F10 : ClassVar[str]
var F11 : ClassVar[str]
var F12 : ClassVar[str]
var F2 : ClassVar[str]
var F3 : ClassVar[str]
var F4 : ClassVar[str]
var F5 : ClassVar[str]
var F6 : ClassVar[str]
var F7 : ClassVar[str]
var F8 : ClassVar[str]
var F9 : ClassVar[str]
var HOME : ClassVar[str]
var INSERT : ClassVar[str]
var LEFT : ClassVar[str]
var PAGE_DOWN : ClassVar[str]
var PAGE_UP : ClassVar[str]
var RIGHT : ClassVar[str]
var SHIFT_TAB : ClassVar[str]
var SUPR : ClassVar[str]
var UP : ClassVar[str]