0

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

yellowhat
  • 429
  • 6
  • 17

1 Answers1

0

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

  • ask ncurses for the string corresponding to "kxIN" with tigetstr (noting that 0 and -1 are special values denoting failure),
  • tell ncurses that this is really a key that getch should report as a keycode, using define_key, and
  • use key_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).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Thanks for your reply. Is your suggestion terminal specific? I am not using xterm but foot. – yellowhat Jul 20 '22 at 06:53
  • Thank you very much for the info. From reading https://codeberg.org/dnkl/foot/issues/670 seems that focus-in/out should be supported in foot. But I do not understand which key code should I capture in python curses. – yellowhat Jul 21 '22 at 06:51
  • Thanks. Following you suggestions: `git clone https://github.com/ThomasDickey/ncurses-snapshots; cd ncurses-snapshots; ./configure; make; ./test/demo_defkey` I do not sse any `kxIN` or `1002` keycode – yellowhat Jul 25 '22 at 19:59