tty --- 終端機控制函式

原始碼:Lib/tty.py


tty 模組定義了將 tty 放入 cbreak 和 raw 模式的函式。

適用: Unix.

因為它需要termios 模組,所以只能在 Unix 上執行。

tty 模組定義了以下函式:

tty.cfmakeraw(mode)

Convert the tty attribute listmode, which is a list like the one returnedbytermios.tcgetattr(), to that of a tty in raw mode.

在 3.12 版被加入.

tty.cfmakecbreak(mode)

Convert the tty attribute listmode, which is a list like the one returnedbytermios.tcgetattr(), to that of a tty in cbreak mode.

This clears theECHO andICANON local mode flags inmode as wellas setting the minimum input to 1 byte with no delay.

在 3.12 版被加入.

在 3.12.2 版的變更:TheICRNL flag is no longer cleared. This matches Linux and macOSsttycbreak behavior and whatsetcbreak() historically did.

tty.setraw(fd,when=termios.TCSAFLUSH)

將檔案描述器fd 的模式更改為 raw。如果when 被省略,則預設為termios.TCSAFLUSH,並傳遞給termios.tcsetattr()termios.tcgetattr() 的回傳值會在設定fd 模式為 raw 之前先儲存起來。此函式回傳這個值。

在 3.12 版的變更:現在的回傳值為原本的 tty 屬性,而不是None

tty.setcbreak(fd,when=termios.TCSAFLUSH)

將檔案描述器fd 的模式更改為 cbreak。如果when 被省略,則預設為termios.TCSAFLUSH,並傳遞給termios.tcsetattr()termios.tcgetattr() 的回傳值會在設定fd 模式為 raw 之前先儲存起來。此函式回傳這個值。

This clears theECHO andICANON local mode flags as well as settingthe minimum input to 1 byte with no delay.

在 3.12 版的變更:現在的回傳值為原本的 tty 屬性,而不是None

在 3.12.2 版的變更:TheICRNL flag is no longer cleared. This restores the behaviorof Python 3.11 and earlier as well as matching what Linux, macOS, & BSDsdescribe in theirstty(1) man pages regarding cbreak mode.

也參考

termios 模組

低階終端機控制介面。