简体   繁体   中英

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.

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 , eg,

#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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM