DECSET ?1004 — Focus in/out events
Make the terminal report when its window gains or loses keyboard focus.
Byte forms
Every common string-literal form so you can paste-and-search either direction.
\\x1b[
\x1b[?1004h (enable) \x1b[?1004l (disable)\\033[
\033[?1004h / \033[?1004l\\e[
\e[?1004h / \e[?1004lESC [
ESC [ ? 1 0 0 4 h / lhex
1b 5b 3f 31 30 30 34 68 / 6cDescription
When enabled, the terminal sends `\x1b[I` to stdin whenever the window gains focus and `\x1b[O` whenever it loses focus. TUIs use this to pause animations, redraw their status bar, or refresh data when the user returns. Vim's `FocusGained` / `FocusLost` autocommands and tmux's `focus-events on` rely on this. Always disable (`?1004l`) on exit; otherwise a crashed TUI keeps spraying `^[[I` / `^[[O` into the user's shell every time they alt-tab.
Spec citation: xterm-ctlseqs (Private mode 1004)
Examples
printf '\033[?1004h' # enable focus events\n# (when window focus changes, terminal emits \x1b[I or \x1b[O)import sys; sys.stdout.write('\x1b[?1004h')fmt.Print("\x1b[?1004h")process.stdout.write('\x1b[?1004h')printf("\x1b[?1004h");Terminal support
- xterm
- yes
- Linux console (fbcon)
- no
- macOS Terminal.app
- no
- iTerm2
- yes
- Windows Terminal
- yes
- cmd.exe / ConPTY
- no
- kitty
- yes
- alacritty
- yes
- WezTerm
- yes
- Ghostty
- yes
- GNOME Terminal
- yes
- Konsole
- yes
- tmux
- no
- GNU screen
- no
| xterm | Linux console (fbcon) | macOS Terminal.app | iTerm2 | Windows Terminal | cmd.exe / ConPTY | kitty | alacritty | WezTerm | Ghostty | GNOME Terminal | Konsole | tmux | GNU screen |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| yes | no | no | yes | yes | no | yes | yes | yes | yes | yes | yes | no | no |
Related sequences
In the family cookbook
DEC cookbook · 6. Focus events & sync update — `?1004`, `?2026`