mouse(3NCURSES)
๐ mouse(3NCURSES) โ the ncurses mouse page, bestie edition ๐
okay so this is mouse(3NCURSES) from the ncurses library calls, and it's literally all about getting mouse events in ncurses. grab a snack, this one's long (part 1 of 2, so don't go anywhere ๐ซถ).
โจ NAME โจ
has_mouse, getmouse, ungetmouse, mousemask, wenclose, mouse_trafo, wmouse_trafo, mouseinterval, mmask_t, MEVENT โ get mouse events in ncurses. that's the whole vibe. ๐ญ
โจ SYNOPSIS โจ
#include <curses.h>
/* data types */
typedef unsigned long mmask_t;
typedef struct {
short id; /* ID to distinguish multiple devices */
int x, y, z; /* event coordinates */
mmask_t bstate; /* button state bits */
} MEVENT;
/* functions */
bool has_mouse(void);
mmask_t mousemask(mmask_t new-mask, mmask_t * old-mask);
int getmouse(MEVENT * event);
int ungetmouse(MEVENT * event);
bool wenclose(const WINDOW * win, int y, int x);
bool mouse_trafo(int * pY, int * pX, bool to-screen);
bool wmouse_trafo(const WINDOW * win,
int * pY, int * pX, bool to-screen);
int mouseinterval(int erval);
โจ DESCRIPTION โจ
ncurses gives you an interface to the mouse or some other pointing device. your app can register that it's interested in those events, and then the library exposes whether a mouse event is available through an input character reading function: that's wgetch(3NCURSES) in the non-wide character curses API, and wget_wch(3NCURSES) in the wide character API. there's a queue that's separate from the keyboard-event queue, and it accumulates all the details of mouse events. the input character reading function reports the KEY_MOUSE key code when a mouse event is ready to be collected. and bestie, listen: a single mouse event queue serves all windows associated with the screen. one queue, every window. iconic. โจ
the MEVENT structure describes a mouse event. its y and x coordinates are screen-, not window-, relative. the bstate member has exactly one bit set, indicating the event type. one bit. that's it. ๐
ncurses ignores mouse events when input is in canonical (โcookedโ) mode, and it produces an error beep when they occur while the library simulates canonical mode in a window โ like with getstr(3NCURSES) (wide-character API besties: that's get_wstr(3NCURSES)), which expects a line feed to terminate its input loop. so yeah, beep. ๐ญ
๐ญ has_mouse
the terminal type or operating system interface has to support the encoding of mouse events. has_mouse returns TRUE if ncurses's mouse driver initialized successfully, and FALSE otherwise. that's the whole thing, no cap. โ
๐ญ mousemask
use mousemask to pick which varieties of mouse event your app wants to receive. by default, ncurses reports no mouse events. zero. nada. so you gotta ask. ๐
- the function returns an updated copy of
new-maskindicating which event types of interest are reportable by the terminal's mouse protocol.
if the screen is not initialized, or the terminal interface does not report mouse events, mousemask returns 0.
- if
old-maskis not a null pointer,mousemaskstores the previous value of the screen's mouse event mask there. throwback moment. ๐
as a side effect, setting a zero mouse mask may turn off the mouse cursor, and setting a nonzero mask may turn it on. whether that actually happens is device-dependent. so like, don't count on it fr fr.
๐ญ Mouse Events
several mouse event types may be selected โ construct a mask by logically โorโ-ing their values. here's the whole menu, bestie, and yes we're listing every single one:
BUTTON1_PRESSEDโ mouse button 1 downBUTTON1_RELEASEDโ mouse button 1 upBUTTON1_CLICKEDโ mouse button 1 clickedBUTTON1_DOUBLE_CLICKEDโ mouse button 1 double clickedBUTTON1_TRIPLE_CLICKEDโ mouse button 1 triple clickedBUTTON2_PRESSEDโ mouse button 2 downBUTTON2_RELEASEDโ mouse button 2 upBUTTON2_CLICKEDโ mouse button 2 clickedBUTTON2_DOUBLE_CLICKEDโ mouse button 2 double clickedBUTTON2_TRIPLE_CLICKEDโ mouse button 2 triple clickedBUTTON3_PRESSEDโ mouse button 3 downBUTTON3_RELEASEDโ mouse button 3 upBUTTON3_CLICKEDโ mouse button 3 clickedBUTTON3_DOUBLE_CLICKEDโ mouse button 3 double clickedBUTTON3_TRIPLE_CLICKEDโ mouse button 3 triple clickedBUTTON4_PRESSEDโ mouse button 4 downBUTTON4_RELEASEDโ mouse button 4 upBUTTON4_CLICKEDโ mouse button 4 clickedBUTTON4_DOUBLE_CLICKEDโ mouse button 4 double clickedBUTTON4_TRIPLE_CLICKEDโ mouse button 4 triple clickedBUTTON5_PRESSEDโ mouse button 5 downBUTTON5_RELEASEDโ mouse button 5 upBUTTON5_CLICKEDโ mouse button 5 clickedBUTTON5_DOUBLE_CLICKEDโ mouse button 5 double clickedBUTTON5_TRIPLE_CLICKEDโ mouse button 5 triple clickedBUTTON_SHIFTโ a shift key was down during button state changeBUTTON_CTRLโ a control key was down during button state changeBUTTON_ALTโ an alt key was down during button state changeALL_MOUSE_EVENTSโ report all button state changesREPORT_MOUSE_POSITIONโ report mouse movement
that's the whole list. go off. ๐ฅ
๐ญ getmouse
when a window is configured to report a non-empty set of event types, calling the input character reading function on that window may return KEY_MOUSE to indicate availability of an enqueued mouse event. to read the event data and remove it from the queue, call getmouse, which returns OK if a mouse event is visible in the given window and ERR otherwise. when getmouse returns OK, it deposits data describing the mouse event in the event pointer you supply. a subsequent getmouse call retrieves the next older event from the queue. so it's a whole little time machine but backwards. โณ
๐ญ ungetmouse
ungetmouse behaves analogously to ungetch(3NCURSES). it pushes a KEY_MOUSE event onto the screen's input queue, and event onto the mouse event queue. un-send, basically. โฉ๏ธ
๐ญ wenclose
wenclose returns TRUE if the pair of screen-relative coordinates (y, x) is enclosed by the given window win, and FALSE otherwise. if win is a pad, wenclose uses its most recent screen coordinates as specified in a prefresh(3NCURSES) or pnoutrefresh(3NCURSES) call. remember where you left it, pad. ๐
wenclose is useful for determining what subset of the screen's windows encloses the location of a mouse event; it is otherwise independent of the ncurses mouse API.
๐ญ wmouse_trafo
wmouse_trafo transforms the given pair of coordinate pointers (pY, pX) from a win-relative basis to a screen-relative one or vice versa, as to-screen is TRUE or FALSE, respectively. stdscr-relative coordinates are not always identical to screen coordinates: curses supports reservation of screen lines at the top and/or bottom for other purposes; see ripoffline(3NCURSES) and slk_init(3NCURSES). so like, math but with vibes. ๐งฎ
if to-screen is TRUE and the pointers (pY, pX) reference coordinates inside win, ncurses updates their values to stdscr-relative coordinates and returns TRUE. if either pY or pX is a null pointer, or (pY, pX) is not inside win, wmouse_trafo returns FALSE.
if to-screen is FALSE and the pointers (pY, pX) reference coordinates inside stdscr, ncurses updates their values to win-relative coordinates and returns TRUE. if either pY or pX is a null pointer, or (pY, pX) is not inside stdscr, wmouse_trafo returns FALSE.
๐ญ mouse_trafo
mouse_trafo applies the wmouse_trafo translation to stdscr. if no screen lines are reserved by ripoffline(3NCURSES) or slk_init(3NCURSES), this is the identity transformation. nothing changes, it just is. โจ
๐ญ mouseinterval
mouseinterval sets the maximum time (in thousandths of a second) that can elapse between press and release events for them to be resolved as a click. an app might interpret button press and release events separated by more than the mouse interval as a โlong pressโ, or, with motion, as a โdragโ. so it's literally the difference between a tap and a whole dramatic gesture. ๐
when ncurses detects a mouse event, it awaits further input activity up to this interval, and then checks for a subsequent mouse event which can be combined with the first event. if the timeout expires without input activity, then no click resolution occurs. calling mouseinterval(0) disables click resolution.
mouseinterval returns the previous interval value. use mouseinterval(-1) to obtain the interval without altering it. peek, don't touch. ๐
the mouse interval is set to one sixth of a second when the corresponding screen is initialized, e.g., in initscr(3NCURSES) or setupterm(3NCURSES).
โจ RETURN VALUE โจ
has_mouse, wenclose, mouse_trafo, and wmouse_trafo return TRUE or FALSE as noted above.
getmouse and ungetmouse return ERR upon failure and OK upon success.
getmouse fails if:
- no mouse driver was initialized,
- the mask of reportable events is zero,
- a mouse event was detected that does not match the mask, or
- no more events remain in the queue.
ungetmouse returns ERR if the event queue is full.
mousemask returns the mask of reportable events.
mouseinterval returns the previous interval value, unless the terminal was not initialized. in that case, it returns the maximum interval value (166). yes, one hundred and sixty-six. that's the ceiling. ๐ญ
okay that's part 1 bestie, hold on, more is comingโฆ ๐ฆ
๐ธ NOTES ๐ธ
okay bestie, important tea before you go wild with this ๐
- The order of the
MEVENTstructure members is not guaranteed. Like, at all. And more fields might get added to the structure in the future, so don't get comfy assuming layout, mmkay? ๐ - Under ncurses, these calls are implemented using either xterm's built-in mouse-tracking API or platform-specific drivers, including:
- Alessandro Rubini's
gpmserver ๐ญ - FreeBSD
sysmouse - OS/2
EMX
- Alessandro Rubini's
- If you're on an unsupported configuration, mouse events are literally not visible to ncurses โ and the
mousemaskfunction will always return0. not a vibe. ๐ - If the terminal type has the (nonstandard) terminfo string capability
XM, ncurses's xterm mouse driver uses it when initializing the terminal for mouse operation. The default, ifXMisn't found, corresponds to private mode1000of xterm.
\E[?1000%?%p1%{1}%=%th%el%;
- ncurses also recognizes xterm's newer private mode
1006. iconic behavior honestly โจ
\E[?1006;1000%?%p1%{1}%=%th%el%;
- The
idmember of the mouse event structure is not presently used โ no terminal type or operating system interface supports reporting events from distinguishable pointing devices. So if you synthesize anMEVENT, use anidof0. ๐ซถ - The
zmember of the mouse event structure is not presently used either. It's intended for use with touch screens (which may be pressure-sensitive) or with 3D-mice/trackballs/power gloves. future tech energy ๐ฎ - The
ALL_MOUSE_EVENTSclass does not includeREPORT_MOUSE_POSITION. They are distinct. For example, in xterm, wheel/scrolling mice send position reports as a sequence of presses of buttons4or5without matching button-releases. not me learning this the hard way ๐ญ
๐ฆ EXTENSIONS ๐ฆ
These functions are ncurses extensions, bestie โ they are not found in SVr4 curses, 4.4BSD curses, or any other previous curses implementation. (SVr4 curses did have a getmouse function, which took no argument and returned an unsigned long. so close and yet so far ๐
)
๐ PORTABILITY ๐
Apps employing the ncurses mouse extension should condition its use on the visibility of the NCURSES_MOUSE_VERSION preprocessor macro. When the interface changes, the macro's value increments. Multiple versions are available when ncurses is configured โ see the section โALTERNATE CONFIGURATIONSโ of ncurses(3NCURSES). The following values may be specified:
1โ has definitions for reserved events. The mask uses 28 bits.2โ adds definitions for button5, removes the definitions for reserved events. The mask uses 29 bits.
๐ HISTORY ๐
SVr4 (1989) added mouse support to its variant of xterm(1). It is mentioned in a few places, with little supporting documentation. lowkey a mystery era ๐
- Its โlibcursesโ manual page lists functions for this feature prototyped in
curses.h.
extern int mouse_set(long int);
extern int mouse_on(long int);
extern int mouse_off(long int);
extern int request_mouse_pos(void);
extern int map_button(unsigned long);
extern void wmouse_position(WINDOW *, int *, int *);
extern unsigned long getmouse(void), getbmap(void);
- Its โterminfoโ manual page lists capabilities for the feature.
buttons btns BT Number of buttons on the mouse
get_mouse getm Gm Curses should get button events
key_mouse kmous Km 0631, Mouse event has occurred
mouse_info minfo Mi Mouse status information
req_mouse_pos reqmp RQ Request mouse position report
- The interface made assumptions (as does ncurses) about the escape sequences sent to and received from the terminal.
For instance, the SVr4 curses library used the get_mouse (getm) capability to tell the terminal which mouse button events it should send, passing the mouse-button bit mask to the terminal. Also, it could ask the terminal where the mouse was using the req_mouse_pos (reqmp) capability.
Those features required a terminal program that had been modified to work with SVr4 curses. They were not part of the X Consortium's xterm. ๐ค
When developing the xterm mouse support for ncurses in September 1995, Eric Raymond was uninterested in using the same interface due to its lack of documentation. Later, in 1998, Mark Hesseling provided support in PDCurses 2.3 using the SVr4 interface. PDCurses, however, does not use video terminals, making it unnecessary to be concerned about compatibility with the escape sequences. history lesson over, you're welcome ๐โจ
๐ BUGS ๐
okay this section is giving "warning, bestie" so pay attention fr fr
- Mouse events from xterm are not ignored in canonical (โcookedโ) mode if they have been enabled by
mousemask. Instead, the xterm mouse report sequence appears in the string read. like, it just shows up. rude. ๐ - An ncurses window must enable
keypad(3NCURSES)to correctly receive mouse event reports from xterm, since they are encoded like function keys. Set the terminal's terminfo capabilitykey_mouse(kmous) to"\E[M"(the beginning of the response from xterm for mouse clicks). Other values ofkey_mouseare permitted under the same assumption โ that is, a mouse report begins with the value of thekey_mouse(kmous) string capability. - Because there are no standard response sequences that serve to identify terminals supporting the xterm mouse protocol, ncurses assumes that if
key_mouse(kmous) is defined in the terminal description, or if the terminal type's primary name or aliases contain the string โxtermโ, then the terminal may send mouse events. ncurses checks thekmouscap-code first, allowing use of newer xterm mouse protocols, such as its private mode1006. slay ๐
๐ซถ SEE ALSO ๐ซถ
ncurses(3NCURSES)inopts(3NCURSES)kernel(3NCURSES)pad(3NCURSES)slk(3NCURSES)curses_variables(3NCURSES)
and that's the whole page, bestie!! ๐ this was ncurses 6.6, dated 2025-11-11, and yes the page is literally called mouse(3NCURSES) โ go forth and handle those mouse events like the main character you are ๐โจ๐ญ
๐ฆ where this page comes from
- ubuntu resolute: ncurses-doc 6.6+20251231-1 ยท MIT/X11
- ubuntu stonking: ncurses-doc 6.6+20260608-2 ยท MIT/X11
- Copyright 2018-2024,2025 Thomas E. Dickey
- Copyright 1998-2015,2017 Free Software Foundation, Inc
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms