跳到主要内容
ansicode

DECSET ?1004 — 焦点进出事件

让终端在窗口获得或失去键盘焦点时上报事件。

字节形式

涵盖所有常见的字符串字面量写法,方便正反查找。

\\x1b[\x1b[?1004h (enable) \x1b[?1004l (disable)
\\033[\033[?1004h / \033[?1004l
\\e[\e[?1004h / \e[?1004l
ESC [ESC [ ? 1 0 0 4 h / l
hex1b 5b 3f 31 30 30 34 68 / 6c

说明

启用后,窗口获得焦点时终端会向 stdin 发送 `\x1b[I`,失去焦点时发送 `\x1b[O`。TUI 用此事件暂停动画、重绘状态栏,或在用户回到窗口时刷新数据。Vim 的 `FocusGained` / `FocusLost` 自动命令、tmux 的 `focus-events on` 均依赖此模式。退出时务必关闭(`?1004l`),否则 TUI 崩溃后用户每次切换窗口都会看到 `^[[I` / `^[[O` 字符串。

规范出处: xterm-ctlseqs (Private mode 1004)

示例

bash
printf '\033[?1004h'   # enable focus events\n# (when window focus changes, terminal emits \x1b[I or \x1b[O)
python
import sys; sys.stdout.write('\x1b[?1004h')
go
fmt.Print("\x1b[?1004h")
javascript
process.stdout.write('\x1b[?1004h')
c
printf("\x1b[?1004h");

终端支持

xterm
支持
Linux console (fbcon)
不支持
macOS Terminal.app
不支持
iTerm2
支持
Windows Terminal
支持
cmd.exe / ConPTY
不支持
kitty
支持
alacritty
支持
WezTerm
支持
Ghostty
支持
GNOME Terminal
支持
Konsole
支持
tmux
不支持
GNU screen
不支持

相关序列

在家族食谱中

DEC 食谱 · 6. 焦点事件与同步更新 —— `?1004`、`?2026`