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