tty --- 終端機控制函式

原始碼:Lib/tty.py


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

適用:Unix。

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

tty 模組定義了以下函式:

tty.cfmakeraw(mode)

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

在 3.12 版被加入.

tty.cfmakecbreak(mode)

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

This clears the ECHO and ICANON local mode flags in mode as well as setting the minimum input to 1 byte with no delay.

在 3.12 版被加入.

在 3.12.2 版的變更: The ICRNL flag is no longer cleared. This matches Linux and macOS stty cbreak behavior and what setcbreak() 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 the ECHO and ICANON local mode flags as well as setting the minimum input to 1 byte with no delay.

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

在 3.12.2 版的變更: The ICRNL flag is no longer cleared. This restores the behavior of Python 3.11 and earlier as well as matching what Linux, macOS, & BSDs describe in their stty(1) man pages regarding cbreak mode.

也參考

termios 模組

低階終端機控制介面。