Gets a single-byte character from the terminal.
Curses Library (libcurses.a)
#include <curses.h>
int getch(void)
int mvgetch(int y,
int x);
int mvwgetch(WINDOW *win,
int y,
int x);
int wgetch(WINDOW *win);
The getch, wgetch, mvgetch, and mvwgetch subroutines read a single-byte character from the terminal associated with the current or specified window. The results are unspecified if the input is not a single-byte character. If the keypad subroutine is enabled, these subroutines respond to the corresponding KEY_ value defined in <curses.h>.
Processing of terminal input is subject to the general rules described in Section 3.5 on page 34.
If echoing is enabled, then the character is echoed as though it were provided as an input argument to the addch subroutine, except for the following characters:
<backspace>,
<left-arrow> and
the current erase character:
The input is interpreted as specified in Section 3.4.3 on page 31 and then the character at the resulting cursor position is deleted as though the delch subroutine was called, except that if the cursor was originally in the first column of the line, then the user is alerted as though the beep subroutine was called.
The user is alerted as though the beep subroutine was called. Information concerning the function keys is not returned to the caller.
Function Keys
If the current or specified window is not a pad, and it has been moved or modified since the last refresh operation, then it will be refreshed before another character is read.
The Importance of Terminal Modes
The output of the getch subroutines is, in part, determined by the mode of the terminal. The following describes the action of the getch subroutines in each type of terminal mode:
Mode | Action of getch Subroutines |
---|---|
NODELAY | Returns a value of ERR if there is no input waiting. |
DELAY | Halts execution until the system passes text through the program. If CBREAK mode is also set, the program stops after receiving one character. If NOCBREAK mode is set, the getch subroutine stops reading after the first new line character. |
HALF-DELAY | Halts execution until a character is typed or a specified time out is reached. If echo is set, the character is also echoed to the window. |
Getting Function Keys
If your program enables the keyboard with the keypad subroutine, and the user presses a function key, the token for that function key is returned instead of raw characters. The possible function keys are defined in the /usr/include/curses.h file. Each #define macro begins with a KEY_ prefix.
If a character is received that could be the beginning of a function key (such as an Escape character) curses sets a timer. If the remainder of the sequence is not received before the timer expires, the character is passed through. Otherwise, the function key's value is returned. For this reason, after a user presses the Esc key there is a delay before the escape is returned to the program. Programmers should not use the Esc key for a single character routine.
Within the getch subroutine, a structure of type timeval, defined in the /usr/include/sys/time.h file, indicates the maximum number of microseconds to wait for the key response to complete.
The ESCDELAY environment variable sets the length of time to wait before timing out and treating the ESC keystroke as the ESC character rather than combining it with other characters in the buffer to create a key sequence. The ESCDELAY environment variable is measured in fifths of a millisecond. If ESCDELAY is 0, the system immediately composes the ESCAPE response without waiting for more information from the buffer. The user may choose any value between 0 and 99,999, inclusive. The default setting for the ESCDELAY environment variable is 500 (one tenth of a second).
Programs that do not want the getch subroutines to set a timer can call the notimeout subroutine. If notimeout is set to TRUE, curses does not distinguish between function keys and characters when retrieving data.
The getch subroutines might not be able to return all function keys because they are not defined in the terminfo database or because the terminal does not transmit a unique code when the key is pressed. The following function keys may be returned by the getch subroutines:
Item | Description |
---|---|
KEY_MIN | Minimum curses key. |
KEY_BREAK | Break key (unreliable). |
KEY_DOWN | Down Arrow key. |
KEY_UP | Up Arrow key. |
KEY_LEFT | Left Arrow key. |
KEY_RIGHT | Right Arrow key. |
KEY_HOME | Home key. |
KEY_BACKSPACE | Backspace. |
KEY_F(n) | Function key Fn, where n is an integer from 0 to 64. |
KEY_DL | Delete line. |
KEY_IL | Insert line. |
KEY_DC | Delete character. |
KEY_IC | Insert character or 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 1 line forward. |
KEY_SR | Scroll 1 line backwards (reverse). |
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 (unreliable). |
KEY_SRESET | Soft (partial) reset (unreliable). |
KEY_RESET | Reset or hard reset (unreliable). |
KEY_PRINT | Print or copy. |
KEY_LL | Home down or bottom (lower left). |
KEY_A1 | Upper-left key of keypad. |
KEY_A3 | Upper-right key of keypad. |
KEY_B2 | Center-key of keypad. |
KEY_C1 | Lower-left key of keypad. |
KEY_C3 | Lower-right key 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 |
Item | Description |
---|---|
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 |
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 arrow key |
KEY_SMESSAGE | shifted message key |
KEY_SMOVE | shifted move key |
KEY_SNEXT | shifted next key |
KEY_SOPTIONS | shifted options key |
KEY_SPREVIOUS | shifted prev key |
KEY_SPRINT | shifted print key |
KEY_SREDO | shifted redo key |
KEY_SREPLACE | shifted replace key |
KEY_SRIGHT | shifted right arrow |
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 |
Item | Description |
---|---|
Column | Specifies the horizontal position to move the logical cursor to before getting the character. |
Line | Specifies the vertical position to move the logical cursor to before getting the character. |
Window | Identifies the window to get the character from and echo it into. |
Upon successful completion, the getch, mvwgetch, and wgetch subroutines, CURSES, and Curses Interface return the single-byte character, KEY_ value, or ERR. When in the nodelay mode and no data is available, ERR is returned.
mvgetch();
mvgetch(20, 30);
WINDOW *my_window;
mvwgetch(my_window, 20, 30);