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