getch, wgetch, mvgetch, mvwgetch, ungetch
The getch, wgetch, mvgetch, mvwgetch, and ungetch routines are used to
get a character from the keyboard.
Syntax
#include <curses.h>
int getch();
int wgetch (WINDOW *win);
int mvgetch(int y, int x);
int mvwgetch(WINDOW *win, int y, int x);
int ungetch(int ch);
Parameters
ch The character to be put back in the input queue for
the next call to getch().
x The x (column) coordinate for the position of the
character to be read.
y The y (row) coordinate for the position of the
character to be read.
win A pointer to the window associated with the
terminal from which the character is to be read.
Return Values
OK Successful completion.
ERR An error occurred. The nodelay() or wtimeout(0)
routine is set, and no input is ready.
Description
The getch() and wgetch() routines get a character from the terminal
associated with the window stdscror window win, respectively. The
mvgetch() and mvwgetch() routines move the cursor to the position
specified in stdscr or win, respectively, then get a character.
If the window is not a pad and has been changed since the last call to
wrefresh(), getch() calls wrefresh() to update the window before the next
character is read.
The setting of certain routines affects how getch() works. For example,
if cbreak() is set, characters typed by the user are immediately
processed. If halfdelay() is set, getch() waits until a character is
typed or returns ERR if no character is typed within the specified
timeout period. This timeout can also be specified for individual
windows with the delay parameter of wtimeout(). A negative value waits
for input; a value of 0 returns ERR if no input is ready; a positive
value blocks until input arrives or the time specified expires (in which
case ERR returns). If nodelay() is set, ERR is returned if no input is
waiting; if not set, getch() waits until input arrives. Each character
will be echoed to the window unless noecho() has been set.
If keypad handling is enabled (keypad() is TRUE), the token for the
function key is returned. If a character is received that could be the
beginning of a function key (for example, ESC), an interbyte timer is
set. If the remainder of the sequence is not received before the time
expires, the character is passed through; otherwise, the value of the
function key is returned. If notimeout() is set, the interbyte timer is
not set.
NOTE The ESCAPE key is typically a prefix key used with function keys.
Since prefix keys used with function keys should not be used as a
single character, ensure that you do not use the ESCAPE key as a
single character.
Table 4-6 shows a list of tokens for the function keys that are
returned by getch() if keypad handling is enabled. (Some terminals may
not support all tokens.)
Table 4-6. Constant Values for Function Keys
---------------------------------------------------------------------------------------------
| | |
| Constant | Description |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_BREAK | Break key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_DOWN | The down arrow key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_UP | The up arrow key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_LEFT | The left arrow key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_RIGHT | The right arrow key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_HOME | Home key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_BACKSPACE | Backspace |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_F0 | Function keys. Space for 64 |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_F(n) | (KEY_F0+(n)) keys is reserved |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_DL | Delete line |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_IL | Insert line |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_DC | Delete character |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_IC | Insert char or enter insert mode |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_EIC | Exit insert char mode |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_CLEAR | Clear screen |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_EOS | Clear to end of screen |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_EOL | Clear to end of line |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SF | Scroll 1 line forward |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SR | Scroll 1 line backwards |
| | |
---------------------------------------------------------------------------------------------
Table 4-6. Constant Values for Function Keys (cont.)
---------------------------------------------------------------------------------------------
| | |
| Constant | Description |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_NPAGE | Next page |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_PPAGE | Previous page |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_STAB | Set tab |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_CTAB | Clear tab |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_CATAB | Clear all tabs |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_ENTER | Enter or send |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SRESET | Soft (partial) reset |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_RESET | Reset or hard reset |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_PRINT | Print or copy |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_LL | Home down or 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_BEG | Beginning key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_CANCEL | Cancel key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_CLOSE | Close key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_COMMAND | Cmd (command) key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_COPY | Copy key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_CREATE | Create key |
| | |
---------------------------------------------------------------------------------------------
Table 4-6. Constant Values for Function Keys (cont.)
---------------------------------------------------------------------------------------------
| | |
| Constant | Description |
| | |
---------------------------------------------------------------------------------------------
| | |
| 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_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_RESTART | Restart key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_RESUME | Resume key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SAVE | Save key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SBEG | Shifted beginning key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SCANCEL | Shifted cancel key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SCOMMAND | Shifted command key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SCOPY | Shifted copy key |
| | |
---------------------------------------------------------------------------------------------
Table 4-6. Constant Values for Function Keys (cont.)
---------------------------------------------------------------------------------------------
| | |
| Constant | Description |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SCREATE | Shifted create key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SDC | Shifted delete char key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SDL | Shifted delete line key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SELECT | Select 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 input key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SLEFT | Shifted left key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SMESSAGES | Shifted messages key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SMOVE | Shifted move key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SNEXT | Shifted next key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SOPTIONS | Shifted options key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SPREVIOUS | Shifted previous key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SPRINT | Shifted print key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SREDO | Shifted redo key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SREPLACE | Shifted replace key |
| | |
---------------------------------------------------------------------------------------------
Table 4-6. Constant Values for Function Keys (cont.)
---------------------------------------------------------------------------------------------
| | |
| Constant | Description |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SRIGHT | Shifted right key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SRSUME | Shifted resume key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SSAVE | Shifted save key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SSUSPEND | Shifted suspend key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SUNDO | Shifted undo key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_SUSPEND | Suspend key |
| | |
---------------------------------------------------------------------------------------------
| | |
| KEY_UNDO | Undo key |
| | |
---------------------------------------------------------------------------------------------
The ungetch() routine delays processing of ch until the next call to
getch().
NOTE The getch(), mvgetch(), and mvwgetch() routines are macros.
Implementation Considerations
The getch(), mvgetch(), mvwgetch(), and wgetch() routines are identical
to XPG/3. The ungetch() routine is a UNIX System V implementation.
See Also
cbreak(), echo(), keypad(), halfdelay(), nodelay(), notimeout(), raw(),
wtimeout()
Portability
The getch(), mvgetch(), mvwgetch(), and wgetch() routines conform to
HP-UX, UNIX System V, and XPG/3. The ungetch() routine conforms to UNIX
System V.