def cbreak(self):
"""
Allow each keystroke to be read immediately after it is pressed.
This is a context manager for :func:`tty.setcbreak`.
This context manager activates 'rare' mode, the opposite of 'cooked'
mode: On entry, :func:`tty.setcbreak` mode is activated disabling
line-buffering of keyboard input and turning off automatic echo of
input as output.
.. note:: You must explicitly print any user input you would like
displayed. If you provide any kind of editing, you must handle
backspace and other line-editing control functions in this mode
as well!
**Normally**, characters received from the keyboard cannot be read
by Python until the *Return* key is pressed. Also known as *cooked* or
*canonical input* mode, it allows the tty driver to provide
line-editing before shuttling the input to your program and is the
(implicit) default terminal mode set by most unix shells before
executing programs.
Technically, this context manager sets the :mod:`termios` attributes
of the terminal attached to :obj:`sys.__stdin__`.
.. note:: :func:`tty.setcbreak` sets ``VMIN = 1`` and ``VTIME = 0``,
see http://www.unixwiz.net/techtips/termios-vmin-vtime.html
"""
if HAS_TTY and self._keyboard_fd is not None:
# Save current terminal mode:
save_mode = termios.tcgetattr(self._keyboard_fd)
save_line_buffered = self._line_buffered
tty.setcbreak(self._keyboard_fd, termios.TCSANOW)
try:
self._line_buffered = False
yield
finally:
# Restore prior mode:
termios.tcsetattr(self._keyboard_fd,
termios.TCSAFLUSH,
save_mode)
self._line_buffered = save_line_buffered
else:
yield
评论列表
文章目录