Skip to content

fix(tui_v3): recover Windows VT input mode after focus loss (closes #633)#652

Open
Kailigithub wants to merge 1 commit into
lsdefine:mainfrom
Kailigithub:fix/issue-633-windows-vt-input-recovery
Open

fix(tui_v3): recover Windows VT input mode after focus loss (closes #633)#652
Kailigithub wants to merge 1 commit into
lsdefine:mainfrom
Kailigithub:fix/issue-633-windows-vt-input-recovery

Conversation

@Kailigithub

Copy link
Copy Markdown
Contributor

Summary

Fixes #633 — TUI v3 input pane becomes unresponsive after switching away from the terminal window and back, on Windows.

Root cause

Some Windows console hosts (Windows Terminal, conhost, legacy cmd.exe) reset the standard input handle's ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) flag when the window loses focus or when another console process attaches/detaches. Without that flag, prompt_toolkit stops receiving arrow keys, function keys, and other ESC-prefixed sequences; the input pane therefore looks frozen until the user restarts the TUI.

Changes

frontends/tui_v3.py — two additive changes, fully backward-compatible on non-Windows:

  1. _enable_windows_vt_mode() now also ORs in ENABLE_VIRTUAL_TERMINAL_INPUT on STD_INPUT_HANDLE alongside the existing VT_OUTPUT flag on STD_OUTPUT_HANDLE/STD_ERROR_HANDLE. Only flips bits that are not already set, so the call is idempotent.

  2. The async maintenance loop, on Windows, now re-invokes _enable_windows_vt_mode() + _enter_utf8_charset() once per second. The loop already ticks every 30ms, so this is purely additive — no new thread. After alt-tabbing back to the terminal, input is restored within ~1s instead of requiring a full TUI restart.

Test plan

  • On Windows 11 + Windows Terminal: open python frontends/tui_v3.py, type some text, alt-tab away for >2s, alt-tab back, confirm arrow keys + Enter still work.
  • On Windows 11 + legacy cmd.exe conhost: same scenario.
  • On macOS / Linux: confirm the loop's new if _IS_WINDOWS branch never fires (no behavior change).
  • Re-run python -m py_compile frontends/tui_v3.py (passes) and ruff check (no new findings introduced).

Closes #633

…sdefine#633)

When a Windows console host (Windows Terminal, conhost, or legacy cmd)
loses focus or sees another console process attach/detach, it sometimes
clears the ENABLE_VIRTUAL_TERMINAL_INPUT flag on the standard input
handle.  Without that flag, prompt_toolkit stops receiving arrow keys,
function keys, and other ESC-prefixed sequences, and the input pane
appears frozen until the user restarts the TUI.

Two changes:

1. _enable_windows_vt_mode() now also sets ENABLE_VIRTUAL_TERMINAL_INPUT
   (0x0200) on STD_INPUT_HANDLE in addition to the existing VT-output
   flag on STD_OUTPUT/STD_ERROR.  Only flips bits that are not already
   set, so it is idempotent on every call.

2. The async maintenance loop on Windows now re-invokes
   _enable_windows_vt_mode() + _enter_utf8_charset() once per second.
   Because the existing loop already runs every 30ms, the new behavior
   is purely additive — no extra thread, no extra tick.  When the user
   alt-tabs away and back, input is restored within ~1s instead of
   requiring a full TUI restart.

Non-Windows callers see no change: both branches are gated on the
_IS_WINDOWS module flag and the VT helpers no-op on other platforms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TUI在切换窗口后,输入框会失效

1 participant