get_wch(3NCURSES)
π NAME π
get_wch, wget_wch, mvget_wch, mvwget_wch, unget_wch β bestie, this is how you grab (or yeet back π«°) a wide character from the curses terminal keyboard buffer. It's a library call, obvi, from the ncurses manual, third section (3NCURSES), so you already know it's the real deal β¨
πΈ SYNOPSIS πΈ
#include <curses.h>
int get_wch(wint_t * wch);
int wget_wch(WINDOW * win, wint_t * wch);
int mvget_wch(int y, int x, wint_t * wch);
int mvwget_wch(WINDOW * win, int y, int x, wint_t * wch);
int unget_wch(const wchar_t wc);
π¦ DESCRIPTION π¦
π Reading Characters π
So wget_wch gathers a key event from the terminal input stream that's associated with a curses window win. And it's givingβ¦ three moods π
- returns
OKif a wide character got read - returns
KEY_CODE_YESif a function key got read - returns
ERRif literally no key event is available
ncurses(3NCURSES) describes the variants of this function, iykyk.
When input is pending, wget_wch stores an integer that identifies the key event into wch. For alphanumeric and punctuation keys, that value corresponds to the character encoding used by the terminal. Using the control key as a modifier β like holding it down while you press and release another key β often results in a distinct code π. The behavior of other keys depends on whether win is in keypad mode; go look at the subsections βKeypad Modeβ and βKey Codesβ in getch(3NCURSES).
If no input is pending, buckle up, it depends:
- If the no-delay flag is set in the window (see
nodelay(3NCURSES)), the function returnsERR. - Otherwise, curses waits until the terminal has input.
- If
cbreak(3NCURSES)orraw(3NCURSES)has been called, this happens after one character is read. - If
nocbreak(3NCURSES)ornoraw(3NCURSES)has been called, it occurs when the next newline is read. (Because the terminal's canonical or βcookedβ mode is line-buffered, multiplewget_wchcalls may then be necessary to empty the input queue. Not me explaining the whole buffer situation π) - If
halfdelay(3NCURSES)has been called, curses waits until input is available or the specified delay elapses.
If echo(3NCURSES) has been called, and the window is not a pad, curses writes the wide character from the input queue to the window (at the cursor position) per the following rules βοΈ
- If the wide character matches the terminal's erase character (see
erasewchar(3NCURSES)), the cursor moves leftward one position and the new position is erased as ifwmove(3NCURSES)and thenwdelch(3NCURSES)were called. When the window's keypad mode is enabled (see below),KEY_LEFTandKEY_BACKSPACEare handled the same way. - curses writes any other wide character to the window, as with
wecho_wchar(3NCURSES). - If the window
winhas been moved or modified since the last call towrefresh(3NCURSES), curses callswrefreshon it.
One more thing π If the wide character is a carriage return and nl(3NCURSES) has been called, wget_wch stores the wide character code for line feed in wch instead. Sneaky little swap, love that for it.
π Ungetting Characters π
unget_wch places wc into the input queue so it gets retrieved by the next call to wget_wch. A single input queue serves all windows associated with the screen β one queue to rule them all, no cap π
β RETURN VALUE β
wget_wch returns:
OKwhen it reads a wide characterKEY_CODE_YESwhen it reads a function key codeERRon failure
wget_wch fails if its timeout expires without any data arriving β which cannot happen if nodelay(3NCURSES) is in effect on the window.
In ncurses, wget_wch also fails if:
- the curses screen has not been initialized,
- (for functions taking a
WINDOWpointer argument)winis a null pointer, - the terminal input stream is closed, in which case
errnois set toEBADF, or - execution was interrupted by a signal, in which case
errnois set toEINTR.
Functions prefixed with βmvβ first perform cursor movement and fail if the position (y, x) is outside the window boundaries. Boundaries are boundaries, bestie π§
unget_wch returns OK on success and ERR on failure. In ncurses, unget_wch fails if:
- the curses screen has not been initialized, or
- there is no more room in the input queue.
π NOTES π
See the βNOTESβ section of wgetch(3NCURSES).
All of these functions except wget_wch and unget_wch may be implemented as macros. (Macros stay messy π)
Unlike wgetch(3NCURSES), wget_wch stores the value of the input character in an additional wch parameter instead of the return value. Different vibe, same goal.
Unlike ungetch, unget_wch cannot distinguish function key codes from conventional character codes. An application can overcome this limitation by pushing function key codes with ungetch and subsequently checking the return value of wget_wch for a match with KEY_CODE_YES. There's your workaround, you're welcome π«Ά
π EXTENSIONS π
See the βEXTENSIONSβ section of wgetch(3NCURSES).
β¨ PORTABILITY β¨
Applications employing ncurses extensions should condition their use on the visibility of the NCURSES_VERSION preprocessor macro. Protect your build, protect your peace π
X/Open Curses Issue 4 describes these functions. It specifies no error conditions for them. (Iconic of it to just not.)
See the βPORTABILITYβ section of wgetch(3NCURSES) regarding the interaction of wget_wch with signal handlers.
π HISTORY π
X/Open Curses Issue 4 (1995) initially specified these functions. The System V Interface Definition Version 4 of the same year specified functions named wgetwch (with its variants) and ungetwch. These were later additions to SVr4.x, not appearing in the first SVr4 (1989). They differ from X/Open's later wget_wch and unget_wch in that:
wgetwchtakes nowchargument, but returns the (wide) key code as anint(with no provision for distinguishing a character code from a function key code); andungetwchtakes a non-constintargument.
History really said βlet me evolve a littleβ π±
π₯ SEE ALSO π₯
getch(3NCURSES) describes comparable functions of the ncurses library in its non-wide-character configuration.
ncurses(3NCURSES), add_wch(3NCURSES), inopts(3NCURSES), move(3NCURSES), refresh(3NCURSES)
And that's the whole page, bestie π this was the ncurses 6.6 manual, dated 2026-04-18, for get_wch(3NCURSES) β go read a wide character, slay, and unget it if you change your mind πβ¨
π¦ where this page comes from
- ubuntu stonking: ncurses-doc 6.6+20260608-2 Β· MIT/X11
- Copyright 2018-2025,2026 Thomas E. Dickey
- Copyright 2002-2016,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