curs_getch(3X)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸ’– NAME πŸ’–

okay so welcome to the curs_getch(3X) manual page, bestie! this is literally all about grabbing characters from the keyboard in curses, and also pushing them back. the functions are getch, wgetch, mvgetch, mvwgetch, ungetch, and has_key β€” and they get (or push back) characters from the curses terminal keyboard buffer. periodt. πŸ’…

✨ SYNOPSIS ✨

#include <curses.h>

int getch(void);
int wgetch(WINDOW * win);
int mvgetch(int y, int x);
int mvwgetch(WINDOW * win, int y, int x);

int ungetch(int c);

/* extension */
int has_key(int c);

🌸 DESCRIPTION 🌸

πŸ“– Reading Characters πŸ“–

wgetch is the main character here, ngl β€” it gathers a key event from the terminal keyboard associated with a curses window win. the ncurses(3X) page describes the variants of this function, iykyk.

when input is pending, wgetch returns an integer that identifies the key event. for alphanumeric and punctuation keys, the space bar, and (usually) the Backspace, Tab, Return, and Escape keys, this value corresponds to the character encoding used by the terminal. using the control key as a modifier β€” like holding it down while pressing and releasing another key β€” often results in a distinct code. the behavior of other keys depends on whether win is in keypad mode; see the β€œKeypad Mode” subsection below, bestie. πŸ¦‹

if no input is pending, then if the no-delay flag is set in the window (see nodelay(3X)), the function returns ERR; otherwise, curses waits until the terminal has input. if cbreak(3X) or raw(3X) has been called, this happens after curses reads one key event. if nocbreak(3X) or noraw(3X) has been called, it occurs when curses reads a newline. (because the terminal's canonical or β€œcooked” mode is line-buffered, multiple wgetch calls may then be necessary to empty the input queue.) if halfdelay(3X) has been called, curses waits until input is available or the specified delay elapses. 😭

if echo(3X) has been called, and the window is not a pad, curses writes the returned character c to the window (at the cursor position) according to these rules, okay?

  • if c matches the terminal's erase character (see erasechar(3X)), and the cursor is not at the window's leftmost column, the cursor moves leftward one position and the new position is erased as if wmove(3X) and then wdelch(3X) were called. when the window's keypad mode is enabled (see below), KEY_LEFT and KEY_BACKSPACE are handled the same way. πŸ’…
  • curses writes any other c to the window, as with wechochar(3X).
  • if the window win has been moved or modified since the last call to wrefresh(3X), curses calls wrefresh on it.

if c is a carriage return and nl(3X) has been called, wgetch returns the character code for line feed instead. no cap. ✨

⌨️ Keypad Mode ⌨️

call keypad(3X) on a window to configure keypad mode when reading input from it. in keypad mode, curses treats key strokes not from the alphabetic section of the keyboard (those corresponding to the ECMA-6 character set β€” see ascii(7) β€” optionally modified by either the control or shift keys) as function keys. (in curses, the term β€œfunction key” includes but is not limited to keycaps engraved with β€œF1”, β€œPF1”, and so on.) if a window is in keypad mode, wgetch translates these key strokes to a numeric code corresponding to the KEY_ symbols listed in the β€œKey Codes” subsection below. if the window is not in keypad mode, the input queue populates with the characters of the function key's escape sequence, which the application must collect individually with multiple wgetch calls. it's giving manual labor, honestly. 😭

  • the curses.h header file declares many function keys whose names begin with KEY_; these object-like macros have integer values outside the range of eight-bit character codes.
  • in ncurses, user-defined function keys are configured with define_key(3X); they have no names, but are also expected to have integer values outside the range of eight-bit character codes.

a variable intended to hold a function key code must thus be of type short or larger. don't try to be cute with a char, bestie, it won't fit. πŸ’€

most terminals you encounter follow the ECMA-48 standard insofar as their function keys produce character sequences prefixed with the escape character ESC. this fact implies that curses cannot distinguish a user's press of the escape key (assuming it sends ESC) from the beginning of a function key's character sequence without waiting to see if, and how soon, further input arrives. so awkward. πŸ™ˆ

  • if the escape sequence matches a string capability defining a function key for the terminal type (such as key_home (khome) or key_up (kuu1)), wgetch returns the function key code corresponding to the unique sequence defined by the terminal.
  • if the escape sequence matches no function keys defined for the terminal type, call wgetch repeatedly to obtain the codes of the individual characters of the sequence, in the order they occurred in the input.
  • if wgetch cannot decide the validity of the input as a function key because it has not read enough characters to disambiguate it, the function waits until it has this information or the escape delay elapses. configure the escape delay with the global variable ESCDELAY, an extension (see section β€œEXTENSIONS” below), or the environment variable of the same name (see section β€œENVIRONMENT” of ncurses(3X)), also an extension.

consequently, a user of a curses application that employs keypad mode may experience a pause or β€œhang” after pressing the escape key while curses collects sufficient characters to disambiguate the input. if the window is in β€œno time-out” mode, the escape delay is effectively infinite; see notimeout(3X). in the event of such a pause, further typing β€œawakens” curses. iconic behavior, honestly. πŸ‘‘

🫢 Ungetting Characters 🫢

ungetch places c into the input queue to be returned by the next call to wgetch. a single input queue serves all windows associated with the screen. so it's a shared vibe, got it. 🫢

πŸ”‘ Key Codes πŸ”‘

the header file curses.h defines the following function key codes. slay. πŸ’…

  • except for the special case of KEY_RESIZE, a window's keypad mode must be enabled for wgetch to read these codes from it.
  • not all of these are necessarily supported on any particular terminal.
  • the naming convention may seem obscure, with some apparent misspellings (such as β€œRSUME” for β€œresume”); the names correspond to the terminfo capability names for the keys, and were standardized before the IBM PC/AT keyboard layout achieved a dominant position in industry.

here's the whole list, no skips:

  • KEY_BREAK β€” Break key
  • KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT β€” Arrow keys
  • KEY_HOME β€” Home key (upward+left arrow)
  • KEY_BACKSPACE β€” Backspace
  • KEY_F0 β€” Function keys; space for 64 keys is reserved
  • KEY_F(n) β€” Function key n where 0 ≀ n ≀ 63
  • KEY_DL β€” Delete line
  • KEY_IL β€” Insert line
  • KEY_DC β€” Delete character
  • KEY_IC β€” Insert character/Enter insert mode
  • KEY_EIC β€” Exit insert character mode
  • KEY_CLEAR β€” Clear screen
  • KEY_EOS β€” Clear to end of screen
  • KEY_EOL β€” Clear to end of line
  • KEY_SF β€” Scroll one line forward
  • KEY_SR β€” Scroll one line backward (reverse)
  • KEY_NPAGE β€” Next page/Page up
  • KEY_PPAGE β€” Previous page/Page down
  • KEY_STAB β€” Set tab
  • KEY_CTAB β€” Clear tab
  • KEY_CATAB β€” Clear all tabs
  • KEY_ENTER β€” Enter/Send
  • KEY_SRESET β€” Soft (partial) reset
  • KEY_RESET β€” (Hard) reset
  • KEY_PRINT β€” Print/Copy
  • KEY_LL β€” Home down/Bottom (lower left)
  • KEY_A1 β€” Upper left of keypad
  • KEY_A3 β€” Upper right of keypad
  • KEY_B2 β€” Center of keypad
  • KEY_C1 β€” Lower left of keypad
  • KEY_C3 β€” Lower right of keypad
  • KEY_BTAB β€” Back tab key
  • KEY_BEG β€” Beg(inning) key
  • KEY_CANCEL β€” Cancel key
  • KEY_CLOSE β€” Close key
  • KEY_COMMAND β€” Cmd (command) key
  • KEY_COPY β€” Copy key
  • KEY_CREATE β€” Create key
  • KEY_END β€” End key
  • KEY_EXIT β€” Exit key
  • KEY_FIND β€” Find key
  • KEY_HELP β€” Help key
  • KEY_MARK β€” Mark key
  • KEY_MESSAGE β€” Message key
  • KEY_MOUSE β€” Mouse event occurred
  • KEY_MOVE β€” Move key
  • KEY_NEXT β€” Next object key
  • KEY_OPEN β€” Open key
  • KEY_OPTIONS β€” Options key
  • KEY_PREVIOUS β€” Previous object key
  • KEY_REDO β€” Redo key
  • KEY_REFERENCE β€” Ref(erence) key
  • KEY_REFRESH β€” Refresh key
  • KEY_REPLACE β€” Replace key
  • KEY_RESIZE β€” Screen resized
  • KEY_RESTART β€” Restart key
  • KEY_RESUME β€” Resume key
  • KEY_SAVE β€” Save key
  • KEY_SELECT β€” Select key
  • KEY_SUSPEND β€” Suspend key
  • KEY_UNDO β€” Undo key

and now the shifted keys, because of course there are shifted keys, bestie:

  • KEY_SBEG β€” Shifted beginning key
  • KEY_SCANCEL β€” Shifted cancel key
  • KEY_SCOMMAND β€” Shifted command key
  • KEY_SCOPY β€” Shifted copy key
  • KEY_SCREATE β€” Shifted create key
  • KEY_SDC β€” Shifted delete character key
  • KEY_SDL β€” Shifted delete line key
  • KEY_SEND β€” Shifted end key
  • KEY_SEOL β€” Shifted clear line key
  • KEY_SEXIT β€” Shifted exit key
  • KEY_SFIND β€” Shifted find key
  • KEY_SHELP β€” Shifted help key
  • KEY_SHOME β€” Shifted home key
  • KEY_SIC β€” Shifted insert key
  • KEY_SLEFT β€” Shifted left arrow key
  • KEY_SMESSAGE β€” Shifted message key
  • KEY_SMOVE β€” Shifted move key
  • KEY_SNEXT β€” Shifted next object key
  • KEY_SOPTIONS β€” Shifted options key
  • KEY_SPREVIOUS β€” Shifted previous object key
  • KEY_SPRINT β€” Shifted print key
  • KEY_SREDO β€” Shifted redo key
  • KEY_SREPLACE β€” Shifted replace key
  • KEY_SRIGHT β€” Shifted right arrow key
  • KEY_SRSUME β€” Shifted resume key
  • KEY_SSAVE β€” Shifted save key
  • KEY_SSUSPEND β€” Shifted suspend key
  • KEY_SUNDO β€” Shifted undo key

many keyboards feature a nine-key directional pad. πŸ’…

                                  β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
                                  β”‚  A1  β”‚  up  β”‚  A3   β”‚
                                  β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
                                  β”‚ left β”‚  B2  β”‚ right β”‚
                                  β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
                                  β”‚  C1  β”‚ down β”‚  C3   β”‚
                                  β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

okay so bestie, two of the symbols in the list above do not correspond to a physical key. yes really. it's giving theoretical. πŸ™ˆ

  • wgetch returns KEY_RESIZE β€” even if the window's keypad mode is disabled β€” if ncurses has handled a SIGWINCH signal since wgetch was called; peep initscr(3X) and resizeterm(3X).
  • wgetch returns KEY_MOUSE to say hey, a mouse event is pending collection; see curs_mouse(3X). Getting this code requires a window's keypad mode to be enabled, because to interpret mouse input (as with xterm(1)'s mouse protocol), ncurses must read an escape sequence, same as with a function key. πŸ’…

🧚 Testing Key Codes

In ncurses, has_key returns a Boolean value indicating whether the terminal type recognizes its parameter as a key code value. See also define_key(3X) and key_defined(3X). slay, that's it. πŸŽ€


✨ RETURN VALUE ✨

wgetch returns a key code identifying the key event as described above β€” which may include KEY_RESIZE or KEY_MOUSE indicating non-key events β€” or ERR on failure. wgetch fails if its timeout expires without any data arriving, which cannot happen if nodelay(3X) is in effect on the window. no cap.

In ncurses, wgetch also fails if

  • the curses screen has not been initialized, 😭
  • (for functions taking a WINDOW pointer argument) win is a null pointer, or
  • execution was interrupted by a signal, in which case the library sets errno to EINTR.

Functions prefixed with β€œmv” first perform cursor movement and fail if the position (y, x) is outside the window boundaries.

ungetch returns OK on success and ERR on failure. In ncurses, ungetch fails if

  • the curses screen has not been initialized, or
  • there is no more room in the input queue. 🫠

has_key returns TRUE or FALSE.


🌸 NOTES 🌸

getch, mvgetch, and mvwgetch may be implemented as macros. (iykyk, that's a whole vibe.)

curses discourages assignment of the ESC key to a discrete function by the programmer, because the library requires a delay while it awaits the potential remainder of a terminal escape sequence. so don't do that.

Some key strokes are indistinguishable from control characters; for example, KEY_ENTER may be the same as ^M, and KEY_BACKSPACE may be the same as ^H or ^?. Consult the terminfo entry for the terminal type to determine whether this is the case; see infocmp(1). Some curses implementations, including ncurses, honor the terminfo key definitions; others treat such control characters specially.

curses distinguishes the Enter keys in the alphabetic and numeric keypad sections of a keyboard because (most) terminals do. KEY_ENTER refers to the key on the numeric keypad and, like other function keys, is reliably recognized only if the window's keypad mode is enabled. πŸ‘‘

  • The terminfo key_enter (kent) capability describes the character (sequence) sent by the Enter key of a terminal's numeric (or similar) keypad.
  • β€œEnter or send” is X/Open Curses's description of this key.

curses treats the Enter or Return key in the alphabetic section of the keyboard differently.

  • It usually produces a control code for carriage return (^M) or line feed (^J).
  • Depending on the terminal mode (raw, cbreak, or canonical), and whether nl(3X) or nonl(3X) has been called, wgetch may return either a carriage return or line feed upon an Enter or Return key stroke.
Use of wgetch with echo(3X) and neither cbreak(3X) nor raw(3X) is not well-defined. ⚠️ bestie that is undefined territory, tread carefully.

Historically, the list of key code macros above was influenced by the keyboard of the AT&T 7300 (also known variously as the β€œ3B1”, β€œSafari 4”, and β€œUNIX PC”), a 1985 machine rich in function keys. Today's computer keyboards are based on that of the IBM PC/AT and tend to have fewer. A curses application can expect such a keyboard to transmit key codes KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_END, KEY_PPAGE (Page Up), KEY_NPAGE (Page Down), KEY_IC (Insert), KEY_DC (Delete), KEY_A1, KEY_A3, KEY_B2, KEY_C1, KEY_C3, and KEY_F(n) for 1 ≀ n ≀ 12. πŸ•°οΈ history lesson but make it iconic.


πŸ’– EXTENSIONS πŸ’–

In ncurses, when a window's β€œno time-out” mode is not set, the ESCDELAY variable configures the duration of the timer used to disambiguate a function key character sequence from a series of key strokes beginning with ESC typed by the user; see curs_variables(3X).

has_key is an ncurses extension, and is not found in SVr4 curses, 4.4BSD curses, or any other previous curses implementation. ncurses original, no remakes.


🌈 PORTABILITY 🌈

Applications employing ncurses extensions should condition their use on the visibility of the NCURSES_VERSION preprocessor macro.

Except as noted in section β€œEXTENSIONS” above, X/Open Curses Issue 4 describes these functions. It specifies no error conditions for them.

SVr4 describes a successful return value only as β€œan integer value other than ERR”.

wgetch reads only single-byte characters. πŸ’€ yeah. wide characters are a whole other page.

The echo behavior of these functions on input of KEY_ or backspace characters is not documented in SVr4 curses.

The behavior of wgetch in the presence of signal handlers is not documented in SVr4 curses and is unspecified by X/Open Curses. In historical curses implementations, it varied depending on whether the operating system's dispatch of a signal to a handler interrupted a read(2) call in progress, and also (in some implementations) whether an input timeout or non-blocking mode had been set. A portable curses application prepares for two cases:

  1. signal receipt does not interrupt wgetch; and
  2. signal receipt interrupts wgetch and causes it to return ERR with errno set to EINTR.

KEY_MOUSE is mentioned in X/Open Curses, along with a few related terminfo capabilities, but no higher-level functions use the feature. The implementation in ncurses is an extension.

KEY_RESIZE and has_key are extensions first implemented for ncurses. By 2022, PDCurses and NetBSD curses had added them along with KEY_MOUSE. trendsetters fr fr.


πŸ•°οΈ HISTORY πŸ•°οΈ

4BSD (1980) introduced wgetch and its variants.

SVr3 (1987) added ungetch.

ncurses 1.9.9g (1996) furnished the has_key extension.


πŸ¦‹ SEE ALSO πŸ¦‹

ECMA-6 β€œ7-bit coded Character Set” <https://ecma-international.org/publications-and-standards/standards/ecma-6/>

ECMA-48 β€œControl Functions for Coded Character Sets” <https://ecma-international.org/publications-and-standards/standards/ecma-48/>

curs_get_wch(3X) describes comparable functions of the ncurses library in its wide-character configuration (ncursesw).

curses(3X), curs_addch(3X), curs_inopts(3X), curs_mouse(3X), curs_move(3X), curs_outopts(3X), curs_refresh(3X), curs_variables(3X), resizeterm(3X), ascii(7)


and that's a wrap on curs_getch(3X), bestie πŸ’œ this one's from ncurses 6.6, dated 2025-11-11 β€” go forth and read your keys, you absolute icon, periodt. πŸŽ€βœ¨

πŸ“¦ where this page comes from