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