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