Skip to main content
ansicode

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[?1004l
ESC [ESC [ ? 1 0 0 4 h / l
hex1b 5b 3f 31 30 30 34 68 / 6c

Description

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

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");

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

Related sequences

In the family cookbook

DEC cookbook · 6. Focus events & sync update — `?1004`, `?2026`