]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_getch.3x
1ef0601388a8fe5f344155b471ee715dbe207b71
[ncurses.git] / man / curs_getch.3x
1 '\" t
2 .\"***************************************************************************
3 .\" Copyright (c) 1998-2014,2015 Free Software Foundation, Inc.              *
4 .\"                                                                          *
5 .\" Permission is hereby granted, free of charge, to any person obtaining a  *
6 .\" copy of this software and associated documentation files (the            *
7 .\" "Software"), to deal in the Software without restriction, including      *
8 .\" without limitation the rights to use, copy, modify, merge, publish,      *
9 .\" distribute, distribute with modifications, sublicense, and/or sell       *
10 .\" copies of the Software, and to permit persons to whom the Software is    *
11 .\" furnished to do so, subject to the following conditions:                 *
12 .\"                                                                          *
13 .\" The above copyright notice and this permission notice shall be included  *
14 .\" in all copies or substantial portions of the Software.                   *
15 .\"                                                                          *
16 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19 .\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20 .\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21 .\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22 .\" THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23 .\"                                                                          *
24 .\" Except as contained in this notice, the name(s) of the above copyright   *
25 .\" holders shall not be used in advertising or otherwise to promote the     *
26 .\" sale, use or other dealings in this Software without prior written       *
27 .\" authorization.                                                           *
28 .\"***************************************************************************
29 .\"
30 .\" $Id: curs_getch.3x,v 1.44 2015/12/20 01:43:03 tom Exp $
31 .TH curs_getch 3X ""
32 .na
33 .hy 0
34 .de bP
35 .IP \(bu 4
36 ..
37 .SH NAME
38 \fBgetch\fR,
39 \fBwgetch\fR,
40 \fBmvgetch\fR,
41 \fBmvwgetch\fR,
42 \fBungetch\fR,
43 \fBhas_key\fR \- get (or push back) characters from \fBcurses\fR terminal keyboard
44 .ad
45 .hy
46 .SH SYNOPSIS
47 \fB#include <curses.h>\fR
48 .PP
49 \fBint getch(void);\fR
50 .br
51 \fBint wgetch(WINDOW *\fP\fIwin);\fR
52 .br
53 \fBint mvgetch(int \fP\fIy\fP\fB, int \fP\fIx\fP\fB);\fR
54 .br
55 \fBint mvwgetch(WINDOW *\fP\fIwin\fP\fB, int \fP\fIy\fP\fB, int \fP\fIx\fP\fB);\fR
56 .br
57 \fBint ungetch(int \fP\fIch\fP\fB);\fR
58 .br
59 \fBint has_key(int \fP\fIch\fP\fB);\fR
60 .br
61 .SH DESCRIPTION
62 .SS Reading characters
63 The \fBgetch\fR, \fBwgetch\fR, \fBmvgetch\fR and \fBmvwgetch\fR, routines read
64 a character from the window.
65 In no-delay mode, if no input is waiting, the value \fBERR\fR is returned.
66 In delay mode, the program waits until the system
67 passes text through to the program.
68 Depending on the setting of \fBcbreak\fR,
69 this is after one character (cbreak mode),
70 or after the first newline (nocbreak mode).
71 In half-delay mode,
72 the program waits until a character is typed or the
73 specified timeout has been reached.
74 .PP
75 If \fBecho\fR is enabled, and the window is not a pad,
76 then the character will also be echoed into the
77 designated window according to the following rules:
78 .bP
79 If the character is the current erase character, left arrow, or backspace,
80 the cursor is moved one space to the left and that screen position is erased
81 as if \fBdelch\fR had been called.
82 .bP
83 If the character value is any other \fBKEY_\fR define, the user is alerted
84 with a \fBbeep\fR call.
85 .bP
86 If the character is a carriage-return,
87 and if \fBnl\fP is enabled,
88 it is translated to a line-feed after echoing.
89 .bP
90 Otherwise the character is simply output to the screen.
91 .PP
92 If the window is not a pad, and it has been moved or modified since the last
93 call to \fBwrefresh\fR, \fBwrefresh\fR will be called before another character
94 is read.
95 .SS Keypad mode
96 .PP
97 If \fBkeypad\fR is \fBTRUE\fR, and a function key is pressed, the token for
98 that function key is returned instead of the raw characters.
99 Possible function
100 keys are defined in \fB<curses.h>\fR as macros with values outside the range
101 of 8-bit characters whose names begin with \fBKEY_\fR.
102 Thus, a variable
103 intended to hold the return value of a function key must be of short size or
104 larger.
105 .PP
106 When a character that could be the beginning of a function key is received
107 (which, on modern terminals, means an escape character),
108 \fBcurses\fR sets a timer.
109 If the remainder of the sequence does not come in within the designated
110 time, the character is passed through;
111 otherwise, the function key value is returned.
112 For this reason, many terminals experience a delay between the time
113 a user presses the escape key and the escape is returned to the program.
114 .SS Ungetting characters
115 .PP
116 The \fBungetch\fR routine places \fIch\fR back onto the input queue to be
117 returned by the next call to \fBwgetch\fR.
118 There is just one input queue for all windows.
119 .PP
120 .SS Predefined key-codes
121 The following special keys are defined in \fB<curses.h>\fR.
122 .bP
123 Except for the special case \fBKEY_RESIZE\fP,
124 it is necessary to enable \fBkeypad\fR for \fBgetch\fP to return these codes.
125 .bP
126 Not all of these are necessarily supported on any particular terminal.
127 .bP
128 The naming convention may seem obscure, with some apparent
129 misspellings (such as "RSUME" for "resume").
130 The names correspond to the long terminfo capability names for the keys,
131 and were defined long ago, in the 1980s.
132 .PP
133 .TS
134 center tab(/) ;
135 l l .
136 \fIName\fR/\fIKey\fR \fIname\fR
137 _
138 KEY_BREAK/Break key
139 KEY_DOWN/The four arrow keys ...
140 KEY_UP
141 KEY_LEFT
142 KEY_RIGHT
143 KEY_HOME/Home key (upward+left arrow)
144 KEY_BACKSPACE/Backspace
145 KEY_F0/T{
146 Function keys; space for 64 keys is reserved.
147 T}
148 KEY_F(\fIn\fR)/T{
149 For 0 \(<= \fIn\fR \(<= 63
150 T}
151 KEY_DL/Delete line
152 KEY_IL/Insert line
153 KEY_DC/Delete character
154 KEY_IC/Insert char or enter insert mode
155 KEY_EIC/Exit insert char mode
156 KEY_CLEAR/Clear screen
157 KEY_EOS/Clear to end of screen
158 KEY_EOL/Clear to end of line
159 KEY_SF/Scroll 1 line forward
160 KEY_SR/Scroll 1 line backward (reverse)
161 KEY_NPAGE/Next page
162 KEY_PPAGE/Previous page
163 KEY_STAB/Set tab
164 KEY_CTAB/Clear tab
165 KEY_CATAB/Clear all tabs
166 KEY_ENTER/Enter or send
167 KEY_SRESET/Soft (partial) reset
168 KEY_RESET/Reset or hard reset
169 KEY_PRINT/Print or copy
170 KEY_LL/Home down or bottom (lower left)
171 KEY_A1/Upper left of keypad
172 KEY_A3/Upper right of keypad
173 KEY_B2/Center of keypad
174 KEY_C1/Lower left of keypad
175 KEY_C3/Lower right of keypad
176 KEY_BTAB/Back tab key
177 KEY_BEG/Beg(inning) key
178 KEY_CANCEL/Cancel key
179 KEY_CLOSE/Close key
180 KEY_COMMAND/Cmd (command) key
181 KEY_COPY/Copy key
182 KEY_CREATE/Create key
183 KEY_END/End key
184 KEY_EXIT/Exit key
185 KEY_FIND/Find key
186 KEY_HELP/Help key
187 KEY_MARK/Mark key
188 KEY_MESSAGE/Message key
189 KEY_MOUSE/Mouse event read
190 KEY_MOVE/Move key
191 KEY_NEXT/Next object key
192 KEY_OPEN/Open key
193 KEY_OPTIONS/Options key
194 KEY_PREVIOUS/Previous object key
195 KEY_REDO/Redo key
196 KEY_REFERENCE/Ref(erence) key
197 KEY_REFRESH/Refresh key
198 KEY_REPLACE/Replace key
199 KEY_RESIZE/Screen resized
200 KEY_RESTART/Restart key
201 KEY_RESUME/Resume key
202 KEY_SAVE/Save key
203 KEY_SBEG/Shifted beginning key
204 KEY_SCANCEL/Shifted cancel key
205 KEY_SCOMMAND/Shifted command key
206 KEY_SCOPY/Shifted copy key
207 KEY_SCREATE/Shifted create key
208 KEY_SDC/Shifted delete char key
209 KEY_SDL/Shifted delete line key
210 KEY_SELECT/Select key
211 KEY_SEND/Shifted end key
212 KEY_SEOL/Shifted clear line key
213 KEY_SEXIT/Shifted exit key
214 KEY_SFIND/Shifted find key
215 KEY_SHELP/Shifted help key
216 KEY_SHOME/Shifted home key
217 KEY_SIC/Shifted input key
218 KEY_SLEFT/Shifted left arrow key
219 KEY_SMESSAGE/Shifted message key
220 KEY_SMOVE/Shifted move key
221 KEY_SNEXT/Shifted next key
222 KEY_SOPTIONS/Shifted options key
223 KEY_SPREVIOUS/Shifted prev key
224 KEY_SPRINT/Shifted print key
225 KEY_SREDO/Shifted redo key
226 KEY_SREPLACE/Shifted replace key
227 KEY_SRIGHT/Shifted right arrow
228 KEY_SRSUME/Shifted resume key
229 KEY_SSAVE/Shifted save key
230 KEY_SSUSPEND/Shifted suspend key
231 KEY_SUNDO/Shifted undo key
232 KEY_SUSPEND/Suspend key
233 KEY_UNDO/Undo key
234 .TE
235 .PP
236 Keypad is arranged like this:
237 .br
238 .TS
239 center allbox tab(/) ;
240 c c c .
241 \fBA1\fR/\fBup\fR/\fBA3\fR
242 \fBleft\fR/\fBB2\fR/\fBright\fR
243 \fBC1\fR/\fBdown\fR/\fBC3\fR
244 .TE
245 .sp
246 A few of these predefined values do \fInot\fP correspond to a real key:
247 .bP
248 .B KEY_RESIZE
249 is returned when the \fBSIGWINCH\fP signal has been detected
250 (see \fBinitscr\fP(3X) and \fBresizeterm\fR(3X)).
251 This code is returned whether or not \fBkeypad\fP has been enabled.
252 .bP
253 .B KEY_MOUSE
254 is returned for mouse-events (see \fBcurs_mouse\fR(3X)).
255 This code relies upon whether or not \fBkeypad\fP(3X) has been enabled,
256 because (e.g., with \fIxterm\fP mouse prototocol) ncurses must
257 read escape sequences,
258 just like a function key.
259 .SS Testing key-codes
260 .PP
261 The \fBhas_key\fR routine takes a key-code value from the above list, and
262 returns \fBTRUE\fP or \fBFALSE\fP according to whether
263 the current terminal type recognizes a key with that value.
264 .PP
265 The library also supports these extensions:
266 .RS 3
267 .TP 5
268 .B define_key
269 defines a key-code for a given string (see \fBdefine_key\fP(3X)).
270 .TP 5
271 .B key_defined
272 checks if there is a key-code defined for a given
273 string (see \fBkey_defined\fP(3X)).
274 .RE
275 .PP
276 .SH RETURN VALUE
277 All routines return the integer \fBERR\fR upon failure and an integer value
278 other than \fBERR\fR (\fBOK\fR in the case of ungetch()) upon successful
279 completion.
280 .RS 3
281 .TP 5
282 \fBungetch\fP
283 returns ERR
284 if there is no more room in the FIFO.
285 .TP
286 \fBwgetch\fP
287 returns ERR
288 if the window pointer is null, or
289 if its timeout expires without having any data.
290 .RE
291 .PP
292 Functions with a "mv" prefix first perform a cursor movement using
293 \fBwmove\fP, and return an error if the position is outside the window,
294 or if the window pointer is null.
295 .SH NOTES
296 Use of the escape key by a programmer for a single character function is
297 discouraged, as it will cause a delay of up to one second while the
298 keypad code looks for a following function-key sequence.
299 .PP
300 Some keys may be the same as commonly used control
301 keys, e.g., \fBKEY_ENTER\fP versus control/M, \fBKEY_BACKSPACE\fP versus control/H.
302 Some curses implementations may differ according to whether they
303 treat these control keys specially (and ignore the terminfo), or
304 use the terminfo definitions.
305 \fBNcurses\fR uses the terminfo definition.
306 If it says that \fBKEY_ENTER\fP is control/M,
307 \fBgetch\fR will return \fBKEY_ENTER\fP
308 when you press control/M.
309 .PP
310 Generally, \fBKEY_ENTER\fP denotes the character(s) sent by the \fIEnter\fP
311 key on the numeric keypad:
312 .bP
313 the terminal description lists the most useful keys,
314 .bP
315 the \fIEnter\fP key on the regular keyboard is already handled by
316 the standard ASCII characters for carriage-return and line-feed,
317 .bP
318 depending on whether \fBnl\fP or \fBnonl\fP was called,
319 pressing "Enter" on the regular keyboard may return either a carriage-return
320 or line-feed, and finally
321 .bP
322 "Enter or send" is the standard description for this key.
323 .PP
324 When using \fBgetch\fR, \fBwgetch\fR, \fBmvgetch\fR, or
325 \fBmvwgetch\fR, nocbreak mode (\fBnocbreak\fR) and echo mode
326 (\fBecho\fR) should not be used at the same time.
327 Depending on the
328 state of the tty driver when each character is typed, the program may
329 produce undesirable results.
330 .PP
331 Note that \fBgetch\fR, \fBmvgetch\fR, and \fBmvwgetch\fR may be macros.
332 .PP
333 Historically, the set of keypad macros was largely defined by the extremely
334 function-key-rich keyboard of the AT&T 7300, aka 3B1, aka Safari 4.
335 Modern
336 personal computers usually have only a small subset of these.
337 IBM PC-style
338 consoles typically support little more than \fBKEY_UP\fR, \fBKEY_DOWN\fR,
339 \fBKEY_LEFT\fR, \fBKEY_RIGHT\fR, \fBKEY_HOME\fR, \fBKEY_END\fR,
340 \fBKEY_NPAGE\fR, \fBKEY_PPAGE\fR, and function keys 1 through 12.
341 The Ins key
342 is usually mapped to \fBKEY_IC\fR.
343 .SH PORTABILITY
344 The *get* functions are described in the XSI Curses standard, Issue 4.
345 They
346 read single-byte characters only.
347 The standard specifies that they return
348 \fBERR\fR on failure, but specifies no error conditions.
349 .PP
350 The echo behavior of these functions on input of \fBKEY_\fR or backspace
351 characters was not specified in the SVr4 documentation.
352 This description is
353 adopted from the XSI Curses standard.
354 .PP
355 The behavior of \fBgetch\fR and friends in the presence of handled signals is
356 unspecified in the SVr4 and XSI Curses documentation.
357 Under historical curses
358 implementations, it varied depending on whether the operating system's
359 implementation of handled signal receipt interrupts a \fBread\fR(2) call in
360 progress or not, and also (in some implementations) depending on whether an
361 input timeout or non-blocking mode has been set.
362 .PP
363 \fBKEY_MOUSE\fP is mentioned in XSI Curses, along with a few related
364 terminfo capabilities, but no higher-level functions use the feature.
365 The implementation in ncurses is an extension.
366 .PP
367 \fBKEY_RESIZE\fP is an extension first implemented for ncurses. 
368 NetBSD curses later added this extension.
369 .PP
370 Programmers concerned about portability should be prepared for either of two
371 cases: (a) signal receipt does not interrupt \fBgetch\fR; (b) signal receipt
372 interrupts \fBgetch\fR and causes it to return ERR with \fBerrno\fR set to
373 \fBEINTR\fR.
374 Under the \fBncurses\fR implementation, handled signals never
375 interrupt \fBgetch\fR.
376 .PP
377 The \fBhas_key\fR function is unique to \fBncurses\fR.
378 We recommend that
379 any code using it be conditionalized on the \fBNCURSES_VERSION\fR feature macro.
380 .SH SEE ALSO
381 \fBcurses\fR(3X),
382 \fBcurs_inopts\fR(3X),
383 \fBcurs_outopts\fR(3X),
384 \fBcurs_mouse\fR(3X),
385 \fBcurs_move\fR(3X),
386 \fBcurs_refresh\fR(3X),
387 \fBresizeterm\fR(3X).
388 .PP
389 Comparable functions in the wide-character (ncursesw) library are
390 described in
391 \fBcurs_get_wch\fR(3X).