From 5725bbbbccacaa143d2e92b30792ab5f850124ce Mon Sep 17 00:00:00 2001 From: Alon Ran Date: Sat, 19 Jul 2025 17:39:46 +0300 Subject: [PATCH] Implement flushing in Windows VT100 input --- src/prompt_toolkit/input/win32.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/prompt_toolkit/input/win32.py b/src/prompt_toolkit/input/win32.py index 1ff3234a3..7667270c0 100644 --- a/src/prompt_toolkit/input/win32.py +++ b/src/prompt_toolkit/input/win32.py @@ -104,8 +104,11 @@ def detach(self) -> ContextManager[None]: def read_keys(self) -> list[KeyPress]: return list(self.console_input_reader.read()) - def flush(self) -> None: - pass + def flush_keys(self) -> None: + if self._use_virtual_terminal_input: + return self.console_input_reader.flush_keys() + else: + return [] @property def closed(self) -> bool: @@ -640,6 +643,20 @@ def read(self) -> Iterable[KeyPress]: self._buffer = [] return result + def flush_keys(self) -> list[KeyPress]: + """ + Flush pending keys and return them. + (Used for flushing the 'escape' key.) + """ + # Flush all pending keys. (This is most important to flush the vt100 + # 'Escape' key early when nothing else follows.) + self._vt100_parser.flush() + + # Return result. + result = self._buffer + self._buffer = [] + return result + def _get_keys( self, read: DWORD, input_records: Array[INPUT_RECORD] ) -> Iterator[str]: