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