English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To change the terminal control in Unix system, we can use tty-related methods in Python. Usingttymodule, we can set two different modes of the terminal. Thegeneratemode andCBREAKmode.
To use the tty module, we should use-Import it
import tty
There are some modules in the tty module, they are-
This method is used to change the terminal mode to raw mode. In raw mode, the cursor moves to a new line but does not perform an Enter operation. Similarly, we do not need to pressEnterThe key sends input to the system, which will automatically send after writing.
This method is used to change the terminal mode to cbreak mode. In this mode, the cursor moves to a new line and we can send input to the system without pressing the Enter key, it will automatically send after writing.
import sys import tty import termios file_desc = sys.stdin.fileno() old_setting = termios.tcgetattr(file_desc) tty.setraw(sys.stdin) for i in range(5) char = sys.stdin.read(1) print("Char: " + str(char)) termios.tcsetattr(file_desc, termios.TCSADRAIN, old_setting)
Output Result
$ python3 example.py Char: K Char: E Char: 5 Char: 2 Char: @