What does python `tty.setraw()` do?

ยท 476 words ยท 3 minute read

tty.setraw() is a function from the tty module in Python that is used to put the terminal into raw mode. In raw mode, the input is made available to the program immediately, without any preprocessing, and special characters such as interrupt signals are not processed by the terminal driver. This mode is useful for applications that require complete control over input handling, such as text editors or terminal-based games.

What does tty.setraw() do? ๐Ÿ”—

The tty.setraw() function modifies the terminal attributes to switch it into raw mode. This involves disabling various input and output processing features that are normally provided by the terminal driver.

How does tty.setraw() Work? ๐Ÿ”—

  1. Fetch the Current Terminal Attributes: Before making any changes, you need to get the current terminal attributes using the termios.tcgetattr(fd) function, where fd is the file descriptor of the terminal (usually sys.stdin.fileno() for standard input).

  2. Modify the Attributes: The tty.setraw() function modifies these attributes to set the terminal into raw mode. This typically involves:

    • Disabling canonical mode (ICANON flag) so that input is made available immediately.
    • Disabling echo (ECHO flag) so that characters typed are not displayed on the screen.
    • Disabling signals (ISIG flag) so that special characters (like interrupt) are not processed.
    • Disabling other input and output processing flags.
  3. Apply the Modified Attributes: After modifying the attributes, you apply the new settings back to the terminal using the termios.tcsetattr(fd, termios.TCSANOW, attrs) function, where attrs are the modified attributes.

tty.setraw() Example Code ๐Ÿ”—

Here’s an example of how you might use tty.setraw() in a Python script:

import sys
import tty
import termios

# Save the original terminal settings
fd = sys.stdin.fileno()
original_attributes = termios.tcgetattr(fd)

try:
    # Set the terminal to raw mode
    tty.setraw(fd)
    print("Terminal is now in raw mode. Press 'q' to quit.")
    # Do something with the terminal in raw mode
    while True:
        ch = sys.stdin.read(1)
        if ch == 'q':  # Exit the loop if 'q' is pressed
            break
        print(f'You pressed: {ch}')
finally:
    # Restore the original terminal settings
    termios.tcsetattr(fd, termios.TCSADRAIN, original_attributes)
    print("Terminal settings restored.")

In this example, the terminal is set to raw mode, allowing you to read each character as it is typed without any processing. The loop continues until the ‘q’ key is pressed, at which point the original terminal settings are restored.

Differences Between Raw Mode and Cbreak Mode ๐Ÿ”—

  • Raw Mode: In raw mode, all input and output processing is disabled. This means that the input is made available to the program immediately and no special characters (like interrupt signals) are processed by the terminal driver.
  • Cbreak Mode: In cbreak mode, input is also made available immediately, but special characters are still processed. This is a less strict mode compared to raw mode.

By using tty.setraw(), you gain full control over the terminal input and output, which is essential for certain types of applications that need to handle user input directly and immediately.