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