1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
4 ****************************************************************************
5 * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc. *
7 * Permission is hereby granted, free of charge, to any person obtaining a *
8 * copy of this software and associated documentation files (the *
9 * "Software"), to deal in the Software without restriction, including *
10 * without limitation the rights to use, copy, modify, merge, publish, *
11 * distribute, distribute with modifications, sublicense, and/or sell *
12 * copies of the Software, and to permit persons to whom the Software is *
13 * furnished to do so, subject to the following conditions: *
15 * The above copyright notice and this permission notice shall be included *
16 * in all copies or substantial portions of the Software. *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
26 * Except as contained in this notice, the name(s) of the above copyright *
27 * holders shall not be used in advertising or otherwise to promote the *
28 * sale, use or other dealings in this Software without prior written *
30 ****************************************************************************
31 * @Id: curs_getch.3x,v 1.36 2011/01/22 19:38:51 tom Exp @
35 <TITLE>curs_getch 3x</TITLE>
36 <link rev=made href="mailto:bug-ncurses@gnu.org">
37 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
40 <H1>curs_getch 3x</H1>
43 <!-- Manpage converted by man2html 3.0.1 -->
44 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG> <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>
51 <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>, <STRONG>mvwgetch</STRONG>, <STRONG>ungetch</STRONG>, <STRONG>has_key</STRONG> - get
52 (or push back) characters from <STRONG>curses</STRONG> terminal keyboard
56 <H2>SYNOPSIS</H2><PRE>
57 <STRONG>#include</STRONG> <STRONG><curses.h></STRONG>
59 <STRONG>int</STRONG> <STRONG>getch(void);</STRONG>
60 <STRONG>int</STRONG> <STRONG>wgetch(WINDOW</STRONG> <STRONG>*win);</STRONG>
61 <STRONG>int</STRONG> <STRONG>mvgetch(int</STRONG> <STRONG>y,</STRONG> <STRONG>int</STRONG> <STRONG>x);</STRONG>
62 <STRONG>int</STRONG> <STRONG>mvwgetch(WINDOW</STRONG> <STRONG>*win,</STRONG> <STRONG>int</STRONG> <STRONG>y,</STRONG> <STRONG>int</STRONG> <STRONG>x);</STRONG>
63 <STRONG>int</STRONG> <STRONG>ungetch(int</STRONG> <STRONG>ch);</STRONG>
64 <STRONG>int</STRONG> <STRONG>has_key(int</STRONG> <STRONG>ch);</STRONG>
68 <H2>DESCRIPTION</H2><PRE>
69 The <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG> and <STRONG>mvwgetch</STRONG>, routines read a
70 character from the window. In no-delay mode, if no input
71 is waiting, the value <STRONG>ERR</STRONG> is returned. In delay mode, the
72 program waits until the system passes text through to the
73 program. Depending on the setting of <STRONG>cbreak</STRONG>, this is af-
74 ter one character (cbreak mode), or after the first new-
75 line (nocbreak mode). In half-delay mode, the program
76 waits until a character is typed or the specified timeout
79 Unless <STRONG>noecho</STRONG> has been set, then the character will also
80 be echoed into the designated window according to the fol-
81 lowing rules: if the character is the current erase char-
82 acter, left arrow, or backspace, the cursor is moved one
83 space to the left and that screen position is erased as if
84 <STRONG>delch</STRONG> had been called. If the character value is any oth-
85 er <STRONG>KEY_</STRONG> define, the user is alerted with a <STRONG>beep</STRONG> call.
86 Otherwise the character is simply output to the screen.
88 If the window is not a pad, and it has been moved or modi-
89 fied since the last call to <STRONG>wrefresh</STRONG>, <STRONG>wrefresh</STRONG> will be
90 called before another character is read.
92 If <STRONG>keypad</STRONG> is <STRONG>TRUE</STRONG>, and a function key is pressed, the to-
93 ken for that function key is returned instead of the raw
94 characters. Possible function keys are defined in <STRONG><curs-</STRONG>
95 <STRONG>es.h></STRONG> as macros with values outside the range of 8-bit
96 characters whose names begin with <STRONG>KEY_</STRONG>. Thus, a variable
97 intended to hold the return value of a function key must
98 be of short size or larger.
100 When a character that could be the beginning of a function
101 key is received (which, on modern terminals, means an es-
102 cape character), <STRONG>curses</STRONG> sets a timer. If the remainder of
103 the sequence does not come in within the designated time,
104 the character is passed through; otherwise, the function
105 key value is returned. For this reason, many terminals
106 experience a delay between the time a user presses the es-
107 cape key and the escape is returned to the program.
109 The <STRONG>ungetch</STRONG> routine places <EM>ch</EM> back onto the input queue to
110 be returned by the next call to <STRONG>wgetch</STRONG>. There is just one
111 input queue for all windows.
113 <STRONG>Function</STRONG> <STRONG>Keys</STRONG>
114 The following function keys, defined in <STRONG><curses.h></STRONG>, might
115 be returned by <STRONG>getch</STRONG> if <STRONG>keypad</STRONG> has been enabled. Note
116 that not all of these are necessarily supported on any
120 <EM>Name</EM> <EM>Key</EM> <EM>name</EM>
122 KEY_DOWN The four arrow keys ...
126 KEY_HOME Home key (upward+left arrow)
127 KEY_BACKSPACE Backspace
128 KEY_F0 Function keys; space for 64 keys
130 KEY_F(<EM>n</EM>) For 0 <= <EM>n</EM> <= 63
133 KEY_DC Delete character
134 KEY_IC Insert char or enter insert mode
135 KEY_EIC Exit insert char mode
136 KEY_CLEAR Clear screen
137 KEY_EOS Clear to end of screen
138 KEY_EOL Clear to end of line
139 KEY_SF Scroll 1 line forward
140 KEY_SR Scroll 1 line backward (reverse)
142 KEY_PPAGE Previous page
145 KEY_CATAB Clear all tabs
146 KEY_ENTER Enter or send
147 KEY_SRESET Soft (partial) reset
148 KEY_RESET Reset or hard reset
149 KEY_PRINT Print or copy
150 KEY_LL Home down or bottom (lower left)
151 KEY_A1 Upper left of keypad
152 KEY_A3 Upper right of keypad
153 KEY_B2 Center of keypad
154 KEY_C1 Lower left of keypad
155 KEY_C3 Lower right of keypad
156 KEY_BTAB Back tab key
157 KEY_BEG Beg(inning) key
158 KEY_CANCEL Cancel key
160 KEY_COMMAND Cmd (command) key
162 KEY_CREATE Create key
168 KEY_MESSAGE Message key
169 KEY_MOUSE Mouse event read
171 KEY_NEXT Next object key
173 KEY_OPTIONS Options key
174 KEY_PREVIOUS Previous object key
176 KEY_REFERENCE Ref(erence) key
177 KEY_REFRESH Refresh key
178 KEY_REPLACE Replace key
179 KEY_RESIZE Screen resized
180 KEY_RESTART Restart key
182 KEY_RESUME Resume key
184 KEY_SBEG Shifted beginning key
185 KEY_SCANCEL Shifted cancel key
186 KEY_SCOMMAND Shifted command key
187 KEY_SCOPY Shifted copy key
188 KEY_SCREATE Shifted create key
189 KEY_SDC Shifted delete char key
190 KEY_SDL Shifted delete line key
191 KEY_SELECT Select key
192 KEY_SEND Shifted end key
193 KEY_SEOL Shifted clear line key
194 KEY_SEXIT Shifted exit key
195 KEY_SFIND Shifted find key
196 KEY_SHELP Shifted help key
197 KEY_SHOME Shifted home key
198 KEY_SIC Shifted input key
199 KEY_SLEFT Shifted left arrow key
200 KEY_SMESSAGE Shifted message key
201 KEY_SMOVE Shifted move key
202 KEY_SNEXT Shifted next key
203 KEY_SOPTIONS Shifted options key
204 KEY_SPREVIOUS Shifted prev key
205 KEY_SPRINT Shifted print key
206 KEY_SREDO Shifted redo key
207 KEY_SREPLACE Shifted replace key
208 KEY_SRIGHT Shifted right arrow
209 KEY_SRSUME Shifted resume key
210 KEY_SSAVE Shifted save key
211 KEY_SSUSPEND Shifted suspend key
212 KEY_SUNDO Shifted undo key
213 KEY_SUSPEND Suspend key
216 Keypad is arranged like this:
219 +-----+------+-------+
220 | <STRONG>A1</STRONG> | <STRONG>up</STRONG> | <STRONG>A3</STRONG> |
221 +-----+------+-------+
222 |<STRONG>left</STRONG> | <STRONG>B2</STRONG> | <STRONG>right</STRONG> |
223 +-----+------+-------+
224 | <STRONG>C1</STRONG> | <STRONG>down</STRONG> | <STRONG>C3</STRONG> |
225 +-----+------+-------+
226 The <STRONG>has_key</STRONG> routine takes a key value from the above list,
227 and returns TRUE or FALSE according to whether the current
228 terminal type recognizes a key with that value. Note that
229 a few values do not correspond to a real key, e.g.,
230 <STRONG>KEY_RESIZE</STRONG> and <STRONG>KEY_MOUSE</STRONG>. See <STRONG><A HREF="resizeterm.3x.html">resizeterm(3x)</A></STRONG> for more de-
231 tails about <STRONG>KEY_RESIZE</STRONG>, and <STRONG><A HREF="curs_mouse.3x.html">curs_mouse(3x)</A></STRONG> for a discus-
232 sion of <STRONG>KEY_MOUSE</STRONG>.
236 <H2>RETURN VALUE</H2><PRE>
237 All routines return the integer <STRONG>ERR</STRONG> upon failure and an
238 integer value other than <STRONG>ERR</STRONG> (<STRONG>OK</STRONG> in the case of ungetch())
239 upon successful completion.
241 <STRONG>ungetch</STRONG>
242 returns an error if there is no more room in
245 <STRONG>wgetch</STRONG>
246 returns an error if the window pointer is
247 null, or if its timeout expires without having
250 Functions with a "mv" prefix first perform a cursor move-
251 ment using <STRONG>wmove</STRONG>, and return an error if the position is
252 outside the window, or if the window pointer is null.
257 Use of the escape key by a programmer for a single charac-
258 ter function is discouraged, as it will cause a delay of
259 up to one second while the keypad code looks for a follow-
260 ing function-key sequence.
262 Note that some keys may be the same as commonly used con-
263 trol keys, e.g., <STRONG>KEY_ENTER</STRONG> versus control/M, <STRONG>KEY_BACKSPACE</STRONG>
264 versus control/H. Some curses implementations may differ
265 according to whether they treat these control keys spe-
266 cially (and ignore the terminfo), or use the terminfo def-
267 initions. <STRONG>Ncurses</STRONG> uses the terminfo definition. If it
268 says that <STRONG>KEY_ENTER</STRONG> is control/M, <STRONG>getch</STRONG> will return
269 <STRONG>KEY_ENTER</STRONG> when you press control/M.
271 Generally, <STRONG>KEY_ENTER</STRONG> denotes the character(s) sent by the
272 <EM>Enter</EM> key on the numeric keypad:
274 <STRONG>o</STRONG> the terminal description lists the most useful keys,
276 <STRONG>o</STRONG> the <EM>Enter</EM> key on the regular keyboard is already han-
277 dled by the standard ASCII characters for carriage-re-
280 <STRONG>o</STRONG> depending on whether <STRONG>nl</STRONG> or <STRONG>nonl</STRONG> was called, pressing
281 "Enter" on the regular keyboard may return either a
282 carriage-return or line-feed, and finally
284 <STRONG>o</STRONG> "Enter or send" is the standard description for this
287 When using <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>, or <STRONG>mvwgetch</STRONG>, nocbreak
288 mode (<STRONG>nocbreak</STRONG>) and echo mode (<STRONG>echo</STRONG>) should not be used at
289 the same time. Depending on the state of the tty driver
290 when each character is typed, the program may produce un-
293 Note that <STRONG>getch</STRONG>, <STRONG>mvgetch</STRONG>, and <STRONG>mvwgetch</STRONG> may be macros.
295 Historically, the set of keypad macros was largely defined
296 by the extremely function-key-rich keyboard of the AT&T
297 7300, aka 3B1, aka Safari 4. Modern personal computers
298 usually have only a small subset of these. IBM PC-style
299 consoles typically support little more than <STRONG>KEY_UP</STRONG>,
300 <STRONG>KEY_DOWN</STRONG>, <STRONG>KEY_LEFT</STRONG>, <STRONG>KEY_RIGHT</STRONG>, <STRONG>KEY_HOME</STRONG>, <STRONG>KEY_END</STRONG>,
301 <STRONG>KEY_NPAGE</STRONG>, <STRONG>KEY_PPAGE</STRONG>, and function keys 1 through 12. The
302 Ins key is usually mapped to <STRONG>KEY_IC</STRONG>.
306 <H2>PORTABILITY</H2><PRE>
307 The *get* functions are described in the XSI Curses stan-
308 dard, Issue 4. They read single-byte characters only.
309 The standard specifies that they return <STRONG>ERR</STRONG> on failure,
310 but specifies no error conditions.
312 The echo behavior of these functions on input of <STRONG>KEY_</STRONG> or
313 backspace characters was not specified in the SVr4 docu-
314 mentation. This description is adopted from the XSI Curs-
317 The behavior of <STRONG>getch</STRONG> and friends in the presence of han-
318 dled signals is unspecified in the SVr4 and XSI Curses
319 documentation. Under historical curses implementations,
320 it varied depending on whether the operating system's im-
321 plementation of handled signal receipt interrupts a
322 <STRONG><A HREF="read.2.html">read(2)</A></STRONG> call in progress or not, and also (in some imple-
323 mentations) depending on whether an input timeout or non-
324 blocking mode has been set.
326 Programmers concerned about portability should be prepared
327 for either of two cases: (a) signal receipt does not in-
328 terrupt <STRONG>getch</STRONG>; (b) signal receipt interrupts <STRONG>getch</STRONG> and
329 causes it to return ERR with <STRONG>errno</STRONG> set to <STRONG>EINTR</STRONG>. Under
330 the <STRONG>ncurses</STRONG> implementation, handled signals never inter-
331 rupt <STRONG>getch</STRONG>.
333 The <STRONG>has_key</STRONG> function is unique to <STRONG>ncurses</STRONG>. We recommend
334 that any code using it be conditionalized on the <STRONG>NCURS-</STRONG>
335 <STRONG>ES_VERSION</STRONG> feature macro.
339 <H2>SEE ALSO</H2><PRE>
340 <STRONG><A HREF="ncurses.3x.html">curses(3x)</A></STRONG>, <STRONG><A HREF="curs_inopts.3x.html">curs_inopts(3x)</A></STRONG>, <STRONG><A HREF="curs_outopts.3x.html">curs_outopts(3x)</A></STRONG>,
341 <STRONG><A HREF="curs_mouse.3x.html">curs_mouse(3x)</A></STRONG>, <STRONG><A HREF="curs_move.3x.html">curs_move(3x)</A></STRONG>, <STRONG><A HREF="curs_refresh.3x.html">curs_refresh(3x)</A></STRONG>, <STRONG>re-</STRONG>
342 <STRONG><A HREF="resizeterm.3x.html">sizeterm(3x)</A></STRONG>.
344 Comparable functions in the wide-character (ncursesw) li-
345 brary are described in <STRONG><A HREF="curs_get_wch.3x.html">curs_get_wch(3x)</A></STRONG>.
349 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>
353 Man(1) output converted with
354 <a href="http://www.oac.uci.edu/indiv/ehood/man2html.html">man2html</a>