]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_getch.3x
ncurses 6.2 - patch 20200906
[ncurses.git] / man / curs_getch.3x
1 '\" t
2 .\"***************************************************************************
3 .\" Copyright 2018-2019,2020 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.55 2020/02/02 23:34:34 tom Exp $
32 .TH curs_getch 3X ""
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\fR,
45 \fBwgetch\fR,
46 \fBmvgetch\fR,
47 \fBmvwgetch\fR,
48 \fBungetch\fR,
49 \fBhas_key\fR \- get (or push back) characters from \fBcurses\fR terminal keyboard
50 .ad
51 .hy
52 .SH SYNOPSIS
53 \fB#include <curses.h>\fR
54 .PP
55 \fBint getch(void);\fR
56 .br
57 \fBint wgetch(WINDOW *\fP\fIwin);\fR
58 .br
59 \fBint mvgetch(int \fP\fIy\fP\fB, int \fP\fIx\fP\fB);\fR
60 .br
61 \fBint mvwgetch(WINDOW *\fP\fIwin\fP\fB, int \fP\fIy\fP\fB, int \fP\fIx\fP\fB);\fR
62 .br
63 \fBint ungetch(int \fP\fIch\fP\fB);\fR
64 .br
65 \fBint has_key(int \fP\fIch\fP\fB);\fR
66 .br
67 .SH DESCRIPTION
68 .SS Reading characters
69 The \fBgetch\fR, \fBwgetch\fR, \fBmvgetch\fR and \fBmvwgetch\fR, routines read
70 a character from the window.
71 In no-delay mode, if no input is waiting, the value \fBERR\fR is returned.
72 In delay mode, the program waits until the system
73 passes text through to the program.
74 Depending on the setting of \fBcbreak\fR,
75 this is after one character (cbreak mode),
76 or after the first newline (nocbreak mode).
77 In half-delay mode,
78 the program waits until a character is typed or the
79 specified timeout has been reached.
80 .PP
81 If \fBecho\fR is enabled, and the window is not a pad,
82 then the character will also be echoed into the
83 designated window according to the following rules:
84 .bP
85 If the character is the current erase character, left arrow, or backspace,
86 the cursor is moved one space to the left and that screen position is erased
87 as if \fBdelch\fR had been called.
88 .bP
89 If the character value is any other \fBKEY_\fR define, the user is alerted
90 with a \fBbeep\fR call.
91 .bP
92 If the character is a carriage-return,
93 and if \fBnl\fP is enabled,
94 it is translated to a line-feed after echoing.
95 .bP
96 Otherwise the character is simply output to the screen.
97 .PP
98 If the window is not a pad, and it has been moved or modified since the last
99 call to \fBwrefresh\fR, \fBwrefresh\fR will be called before another character
100 is read.
101 .SS Keypad mode
102 .PP
103 If \fBkeypad\fR is \fBTRUE\fR, and a function key is pressed, the token for
104 that function key is returned instead of the raw characters:
105 .bP
106 The predefined function
107 keys are listed in \fB<curses.h>\fR as macros with values outside the range
108 of 8-bit characters.
109 Their names begin with \fBKEY_\fR.
110 .bP
111 Other (user-defined) function keys which may be defined
112 using \fBdefine_key\fP(3X)
113 have no names, but also are expected to have values outside the range of
114 8-bit characters.
115 .PP
116 Thus, a variable
117 intended to hold the return value of a function key must be of short size or
118 larger.
119 .PP
120 When a character that could be the beginning of a function key is received
121 (which, on modern terminals, means an escape character),
122 \fBcurses\fR sets a timer.
123 If the remainder of the sequence does not come in within the designated
124 time, the character is passed through;
125 otherwise, the function key value is returned.
126 For this reason, many terminals experience a delay between the time
127 a user presses the escape key and the escape is returned to the program.
128 .PP
129 In \fBncurses\fP, the timer normally expires after
130 the value in \fBESCDELAY\fP (see \fBcurs_variables\fP(3X)).
131 If \fBnotimeout\fP is \fBTRUE\fP, the timer does not expire;
132 it is an infinite (or very large) value.
133 Because function keys usually begin with an escape character,
134 the terminal may appear to hang in notimeout mode after pressing the escape key
135 until another key is pressed.
136 .SS Ungetting characters
137 .PP
138 The \fBungetch\fR routine places \fIch\fR back onto the input queue to be
139 returned by the next call to \fBwgetch\fR.
140 There is just one input queue for all windows.
141 .PP
142 .SS Predefined key-codes
143 The following special keys are defined in \fB<curses.h>\fR.
144 .bP
145 Except for the special case \fBKEY_RESIZE\fP,
146 it is necessary to enable \fBkeypad\fR 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 .TS
156 center tab(/) ;
157 l l .
158 \fIName\fR/\fIKey\fR \fIname\fR
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\fR)/T{
171 For 0 \(<= \fIn\fR \(<= 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 read
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 input 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
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 .PP
258 Keypad is arranged like this:
259 .br
260 .TS
261 center allbox tab(/) ;
262 c c c .
263 \fBA1\fR/\fBup\fR/\fBA3\fR
264 \fBleft\fR/\fBB2\fR/\fBright\fR
265 \fBC1\fR/\fBdown\fR/\fBC3\fR
266 .TE
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\fR(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\fR(3X)).
277 This code relies upon whether or not \fBkeypad\fP(3X) has been enabled,
278 because (e.g., with \fIxterm\fP mouse prototocol) ncurses must
279 read escape sequences,
280 just like a function key.
281 .SS Testing key-codes
282 .PP
283 The \fBhas_key\fR routine takes a key-code value from the above list, and
284 returns \fBTRUE\fP or \fBFALSE\fP according to whether
285 the current terminal type recognizes a key with that value.
286 .PP
287 The library also supports these extensions:
288 .RS 3
289 .TP 5
290 .B define_key
291 defines a key-code for a given string (see \fBdefine_key\fP(3X)).
292 .TP 5
293 .B key_defined
294 checks if there is a key-code defined for a given
295 string (see \fBkey_defined\fP(3X)).
296 .RE
297 .PP
298 .SH RETURN VALUE
299 All routines return the integer \fBERR\fR upon failure and an integer value
300 other than \fBERR\fR (\fBOK\fR in the case of \fBungetch\fP) upon successful
301 completion.
302 .RS 3
303 .TP 5
304 \fBungetch\fP
305 returns \fBERR\fP
306 if there is no more room in the FIFO.
307 .TP
308 \fBwgetch\fP
309 returns \fBERR\fP
310 if the window pointer is null, or
311 if its timeout expires without having any data, or
312 if the execution was interrupted by a signal (\fBerrno\fR will be set to
313 \fBEINTR\fR).
314 .RE
315 .PP
316 Functions with a \*(``mv\*('' prefix first perform a cursor movement using
317 \fBwmove\fP, and return an error if the position is outside the window,
318 or if the window pointer is null.
319 .SH NOTES
320 Use of the escape key by a programmer for a single character function is
321 discouraged, as it will cause a delay of up to one second while the
322 keypad code looks for a following function-key sequence.
323 .PP
324 Some keys may be the same as commonly used control
325 keys, e.g.,
326 \fBKEY_ENTER\fP versus control/M,
327 \fBKEY_BACKSPACE\fP versus control/H.
328 Some curses implementations may differ according to whether they
329 treat these control keys specially (and ignore the terminfo), or
330 use the terminfo definitions.
331 \fBNcurses\fR uses the terminfo definition.
332 If it says that \fBKEY_ENTER\fP is control/M,
333 \fBgetch\fR will return \fBKEY_ENTER\fP
334 when you press control/M.
335 .PP
336 Generally, \fBKEY_ENTER\fP denotes the character(s) sent by the \fIEnter\fP
337 key on the numeric keypad:
338 .bP
339 the terminal description lists the most useful keys,
340 .bP
341 the \fIEnter\fP key on the regular keyboard is already handled by
342 the standard ASCII characters for carriage-return and line-feed,
343 .bP
344 depending on whether \fBnl\fP or \fBnonl\fP was called,
345 pressing \*(``Enter\*('' on the regular keyboard
346 may return either a carriage-return or line-feed, and finally
347 .bP
348 \*(``Enter or send\*('' is the standard description for this key.
349 .PP
350 When using \fBgetch\fR, \fBwgetch\fR, \fBmvgetch\fR, or
351 \fBmvwgetch\fR, nocbreak mode (\fBnocbreak\fR) and echo mode
352 (\fBecho\fR) should not be used at the same time.
353 Depending on the
354 state of the tty driver when each character is typed, the program may
355 produce undesirable results.
356 .PP
357 Note that \fBgetch\fR, \fBmvgetch\fR, and \fBmvwgetch\fR may be macros.
358 .PP
359 Historically, the set of keypad macros was largely defined by the extremely
360 function-key-rich keyboard of the AT&T 7300, aka 3B1, aka Safari 4.
361 Modern
362 personal computers usually have only a small subset of these.
363 IBM PC-style
364 consoles typically support little more than \fBKEY_UP\fR, \fBKEY_DOWN\fR,
365 \fBKEY_LEFT\fR, \fBKEY_RIGHT\fR, \fBKEY_HOME\fR, \fBKEY_END\fR,
366 \fBKEY_NPAGE\fR, \fBKEY_PPAGE\fR, and function keys 1 through 12.
367 The Ins key
368 is usually mapped to \fBKEY_IC\fR.
369 .SH PORTABILITY
370 The *get* functions are described in the XSI Curses standard, Issue 4.
371 They
372 read single-byte characters only.
373 The standard specifies that they return
374 \fBERR\fR on failure, but specifies no error conditions.
375 .PP
376 The echo behavior of these functions on input of \fBKEY_\fR or backspace
377 characters was not specified in the SVr4 documentation.
378 This description is
379 adopted from the XSI Curses standard.
380 .PP
381 The behavior of \fBgetch\fR and friends in the presence of handled signals is
382 unspecified in the SVr4 and XSI Curses documentation.
383 Under historical curses
384 implementations, it varied depending on whether the operating system's
385 implementation of handled signal receipt interrupts a \fBread\fR(2) call in
386 progress or not, and also (in some implementations) depending on whether an
387 input timeout or non-blocking mode has been set.
388 .PP
389 \fBKEY_MOUSE\fP is mentioned in XSI Curses, along with a few related
390 terminfo capabilities, but no higher-level functions use the feature.
391 The implementation in ncurses is an extension.
392 .PP
393 \fBKEY_RESIZE\fP is an extension first implemented for ncurses.
394 NetBSD curses later added this extension.
395 .PP
396 Programmers concerned about portability should be prepared for either of two
397 cases: (a) signal receipt does not interrupt \fBgetch\fR; (b) signal receipt
398 interrupts \fBgetch\fR and causes it to return \fBERR\fP with \fBerrno\fR set to
399 \fBEINTR\fR.
400 .PP
401 The \fBhas_key\fR function is unique to \fBncurses\fR.
402 We recommend that
403 any code using it be conditionalized on the \fBNCURSES_VERSION\fR feature macro.
404 .SH SEE ALSO
405 \fBcurses\fR(3X),
406 \fBcurs_inopts\fR(3X),
407 \fBcurs_outopts\fR(3X),
408 \fBcurs_mouse\fR(3X),
409 \fBcurs_move\fR(3X),
410 \fBcurs_refresh\fR(3X),
411 \fBcurs_variables\fR(3X),
412 \fBresizeterm\fR(3X).
413 .PP
414 Comparable functions in the wide-character (ncursesw) library are
415 described in
416 \fBcurs_get_wch\fR(3X).