AlgoRythm
AlgoRythm

Reputation: 1389

ncurses detect when mouse leaves window

When I research mouse interfacing with ncurses, I see many options, but I do not see any way to detect when the mouse has left the program window. The window is the window of the terminal emulator, not an ncurses window.

Upvotes: 2

Views: 329

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54505

That's not in the repertoire of ncurses' mouse interface, but for some terminals you could set them up to send xterm's leave- and enter-window control sequences, which your program could read either byte-by-byte using getch, or by using define_key to associate the responses as a "function key".

XTerm Control Sequences lists in the section on FocusIn/FocusOut:

FocusIn/FocusOut can be combined with any of the mouse events since it uses a different protocol. When set, it causes xterm to send CSI I when the terminal gains focus, and CSI O when it loses focus.

That is enabled with

CSI ? Pm h
          DEC Private Mode Set (DECSET).
...
            Ps = 1 0 0 4  -> Send FocusIn/FocusOut events, xterm.

for instance,

printf("\033[?1004h");
fflush(stdout);

(a few other terminals implement this, but since they do not document their behavior, you'll have to experiment to find out whether this applies to the terminal you happen to be using).

In ncurses, you could associate the responses with define_key, e.g.,

#define KEY_FOCUS_IN     1001
#define KEY_FOCUS_OUT    1002

define_key("\033[I", KEY_FOCUS_IN);
define_key("\033[O", KEY_FOCUS_OUT);

and (if keypad is enabled), detect those values in your program as the return value from getch.

Upvotes: 2

Related Questions