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