ncurses 检测鼠标何时离开 window

ncurses detect when mouse leaves window

当我研究鼠标与 ncurses 的接口时,我看到了很多选项,但我没有看到任何方法来检测鼠标何时离开程序 window。 window 是终端仿真器的 window,不是 ncurses window。

这不在 ncurses 鼠标界面的 repertoire 中,但对于某些终端,您可以将它们设置为发送 xterm 的离开和输入-window 控制序列,您的程序可以读取这些序列使用 getch 逐字节,或使用 define_key 将响应关联为 "function key".

XTerm Control SequencesFocusIn/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.

启用

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

例如,

printf("3[?1004h");
fflush(stdout);

(其他一些终端也实现了这一点,但由于它们没有记录它们的行为,因此您必须进行试验以确定这是否适用于您碰巧正在使用的终端)。

在 ncurses 中,您可以将响应与 define_key 相关联,例如,

#define KEY_FOCUS_IN     1001
#define KEY_FOCUS_OUT    1002

define_key("3[I", KEY_FOCUS_IN);
define_key("3[O", KEY_FOCUS_OUT);

和(如果 keypad 已启用),将程序中的这些值检测为来自 getch.

的 return 值