is there a key to detect when the terminal is focused?
For example the KEY_RESIZE is used to detect when the terminal is resized.
Thanks
There's no predefined key, but with ncurses it is possible to define keys as done in the examples xterm-1002 and xterm-1003, which use this xterm feature
xterm+focus|xterm focus-in/out event "keys",
kxIN=\E[I, kxOUT=\E[O,
documented in XTerm Control Sequences. There's no predefined keycode, but a program could
"kxIN" with tigetstr (noting that 0 and -1 are special values denoting failure),getch should report as a keycode, using define_key, andkey_defined to retrieve the resulting keycode.The ncurses test-program demo_defkey uses define_key and key_defined to demonstrate how to use these functions. That's in C, of course. From python that's also doable (see for example this).
Other terminals implement a subset of xterm's control sequences. OP comments about foot, which happens to recognize mode 1004 (focus in/out). One could make a customized terminal description
foot-focus|foot with focus-in/out,
use=xterm+focus, use=foot,
and use tic to compile that. (Starting with the terminal description for foot is recommended; just setting TERM=xterm-1002, etc., tends to lead to disappointment on the part of users).