3 ****************************************************************************
4 * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc. *
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: *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
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. *
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 *
29 ****************************************************************************
30 * @Id: curs_getch.3x,v 1.51 2017/11/21 00:45:48 tom Exp @
32 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
35 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
36 <meta name="generator" content="Manpage converted by man2html - see https://invisible-island.net/scripts/readme.html#others_scripts">
37 <TITLE>curs_getch 3x</TITLE>
38 <link rev=made href="mailto:bug-ncurses@gnu.org">
39 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
42 <H1 class="no-header">curs_getch 3x</H1>
44 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG> <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>
49 </PRE><H2><a name="h2-NAME">NAME</a></H2><PRE>
50 <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>, <STRONG>mvwgetch</STRONG>, <STRONG>ungetch</STRONG>, <STRONG>has_key</STRONG> - get (or push back)
51 characters from <STRONG>curses</STRONG> terminal keyboard
54 </PRE><H2><a name="h2-SYNOPSIS">SYNOPSIS</a></H2><PRE>
55 <STRONG>#include</STRONG> <STRONG><curses.h></STRONG>
57 <STRONG>int</STRONG> <STRONG>getch(void);</STRONG>
58 <STRONG>int</STRONG> <STRONG>wgetch(WINDOW</STRONG> <STRONG>*</STRONG><EM>win);</EM>
59 <STRONG>int</STRONG> <STRONG>mvgetch(int</STRONG> <EM>y</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>x</EM><STRONG>);</STRONG>
60 <STRONG>int</STRONG> <STRONG>mvwgetch(WINDOW</STRONG> <STRONG>*</STRONG><EM>win</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>y</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>x</EM><STRONG>);</STRONG>
61 <STRONG>int</STRONG> <STRONG>ungetch(int</STRONG> <EM>ch</EM><STRONG>);</STRONG>
62 <STRONG>int</STRONG> <STRONG>has_key(int</STRONG> <EM>ch</EM><STRONG>);</STRONG>
65 </PRE><H2><a name="h2-DESCRIPTION">DESCRIPTION</a></H2><PRE>
67 </PRE><H3><a name="h3-Reading-characters">Reading characters</a></H3><PRE>
68 The <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG> and <STRONG>mvwgetch</STRONG>, routines read a character from
69 the window. In no-delay mode, if no input is waiting, the value <STRONG>ERR</STRONG> is
70 returned. In delay mode, the program waits until the system passes
71 text through to the program. Depending on the setting of <STRONG>cbreak</STRONG>, this
72 is after one character (cbreak mode), or after the first newline
73 (nocbreak mode). In half-delay mode, the program waits until a charac-
74 ter is typed or the specified timeout has been reached.
76 If <STRONG>echo</STRONG> is enabled, and the window is not a pad, then the character
77 will also be echoed into the designated window according to the follow-
80 <STRONG>o</STRONG> If the character is the current erase character, left arrow, or
81 backspace, the cursor is moved one space to the left and that
82 screen position is erased as if <STRONG>delch</STRONG> had been called.
84 <STRONG>o</STRONG> If the character value is any other <STRONG>KEY_</STRONG> define, the user is alert-
85 ed with a <STRONG>beep</STRONG> call.
87 <STRONG>o</STRONG> If the character is a carriage-return, and if <STRONG>nl</STRONG> is enabled, it is
88 translated to a line-feed after echoing.
90 <STRONG>o</STRONG> Otherwise the character is simply output to the screen.
92 If the window is not a pad, and it has been moved or modified since the
93 last call to <STRONG>wrefresh</STRONG>, <STRONG>wrefresh</STRONG> will be called before another character
97 </PRE><H3><a name="h3-Keypad-mode">Keypad mode</a></H3><PRE>
98 If <STRONG>keypad</STRONG> is <STRONG>TRUE</STRONG>, and a function key is pressed, the token for that
99 function key is returned instead of the raw characters:
101 <STRONG>o</STRONG> The predefined function keys are listed in <STRONG><curses.h></STRONG> as macros
102 with values outside the range of 8-bit characters. Their names be-
103 gin with <STRONG>KEY_</STRONG>.
105 <STRONG>o</STRONG> Other (user-defined) function keys which may be defined using <STRONG>de-</STRONG>
106 <STRONG><A HREF="define_key.3x.html">fine_key(3x)</A></STRONG> have no names, but also are expected to have values
107 outside the range of 8-bit characters.
109 Thus, a variable intended to hold the return value of a function key
110 must be of short size or larger.
112 When a character that could be the beginning of a function key is re-
113 ceived (which, on modern terminals, means an escape character), <STRONG>curses</STRONG>
114 sets a timer. If the remainder of the sequence does not come in within
115 the designated time, the character is passed through; otherwise, the
116 function key value is returned. For this reason, many terminals expe-
117 rience a delay between the time a user presses the escape key and the
118 escape is returned to the program.
120 In <STRONG>ncurses</STRONG>, the timer normally expires after the value in <STRONG>ESCDELAY</STRONG> (see
121 <STRONG><A HREF="curs_variables.3x.html">curs_variables(3x)</A></STRONG>). If <STRONG>notimeout</STRONG> is <STRONG>TRUE</STRONG>, the timer does not expire;
122 it is an infinite (or very large) value. Because function keys usually
123 begin with an escape character, the terminal may appear to hang in no-
124 timeout mode after pressing the escape key until another key is
128 </PRE><H3><a name="h3-Ungetting-characters">Ungetting characters</a></H3><PRE>
129 The <STRONG>ungetch</STRONG> routine places <EM>ch</EM> back onto the input queue to be returned
130 by the next call to <STRONG>wgetch</STRONG>. There is just one input queue for all win-
134 </PRE><H3><a name="h3-Predefined-key-codes">Predefined key-codes</a></H3><PRE>
135 The following special keys are defined in <STRONG><curses.h></STRONG>.
137 <STRONG>o</STRONG> Except for the special case <STRONG>KEY_RESIZE</STRONG>, it is necessary to enable
138 <STRONG>keypad</STRONG> for <STRONG>getch</STRONG> to return these codes.
140 <STRONG>o</STRONG> Not all of these are necessarily supported on any particular termi-
143 <STRONG>o</STRONG> The naming convention may seem obscure, with some apparent mis-
144 spellings (such as "RSUME" for "resume"). The names correspond to
145 the long terminfo capability names for the keys, and were defined
146 long ago, in the 1980s.
148 <EM>Name</EM> <EM>Key</EM> <EM>name</EM>
149 -------------------------------------------------
151 KEY_DOWN The four arrow keys ...
155 KEY_HOME Home key (upward+left arrow)
156 KEY_BACKSPACE Backspace
157 KEY_F0 Function keys; space for 64 keys
159 KEY_F(<EM>n</EM>) For 0 <= <EM>n</EM> <= 63
162 KEY_DC Delete character
163 KEY_IC Insert char or enter insert mode
164 KEY_EIC Exit insert char mode
165 KEY_CLEAR Clear screen
166 KEY_EOS Clear to end of screen
167 KEY_EOL Clear to end of line
168 KEY_SF Scroll 1 line forward
169 KEY_SR Scroll 1 line backward (reverse)
171 KEY_PPAGE Previous page
174 KEY_CATAB Clear all tabs
175 KEY_ENTER Enter or send
176 KEY_SRESET Soft (partial) reset
177 KEY_RESET Reset or hard reset
178 KEY_PRINT Print or copy
179 KEY_LL Home down or bottom (lower left)
180 KEY_A1 Upper left of keypad
181 KEY_A3 Upper right of keypad
183 KEY_B2 Center of keypad
184 KEY_C1 Lower left of keypad
185 KEY_C3 Lower right of keypad
186 KEY_BTAB Back tab key
187 KEY_BEG Beg(inning) key
188 KEY_CANCEL Cancel key
190 KEY_COMMAND Cmd (command) key
192 KEY_CREATE Create key
198 KEY_MESSAGE Message key
199 KEY_MOUSE Mouse event read
201 KEY_NEXT Next object key
203 KEY_OPTIONS Options key
204 KEY_PREVIOUS Previous object key
206 KEY_REFERENCE Ref(erence) key
207 KEY_REFRESH Refresh key
208 KEY_REPLACE Replace key
209 KEY_RESIZE Screen resized
210 KEY_RESTART Restart key
211 KEY_RESUME Resume key
213 KEY_SBEG Shifted beginning key
214 KEY_SCANCEL Shifted cancel key
215 KEY_SCOMMAND Shifted command key
216 KEY_SCOPY Shifted copy key
217 KEY_SCREATE Shifted create key
218 KEY_SDC Shifted delete char key
219 KEY_SDL Shifted delete line key
220 KEY_SELECT Select key
221 KEY_SEND Shifted end key
222 KEY_SEOL Shifted clear line key
223 KEY_SEXIT Shifted exit key
224 KEY_SFIND Shifted find key
225 KEY_SHELP Shifted help key
226 KEY_SHOME Shifted home key
227 KEY_SIC Shifted input key
228 KEY_SLEFT Shifted left arrow key
229 KEY_SMESSAGE Shifted message key
230 KEY_SMOVE Shifted move key
231 KEY_SNEXT Shifted next key
232 KEY_SOPTIONS Shifted options key
233 KEY_SPREVIOUS Shifted prev key
234 KEY_SPRINT Shifted print key
235 KEY_SREDO Shifted redo key
236 KEY_SREPLACE Shifted replace key
237 KEY_SRIGHT Shifted right arrow
238 KEY_SRSUME Shifted resume key
239 KEY_SSAVE Shifted save key
240 KEY_SSUSPEND Shifted suspend key
241 KEY_SUNDO Shifted undo key
242 KEY_SUSPEND Suspend key
245 Keypad is arranged like this:
247 +-----+------+-------+
248 | <STRONG>A1</STRONG> | <STRONG>up</STRONG> | <STRONG>A3</STRONG> |
249 +-----+------+-------+
250 |<STRONG>left</STRONG> | <STRONG>B2</STRONG> | <STRONG>right</STRONG> |
251 +-----+------+-------+
252 | <STRONG>C1</STRONG> | <STRONG>down</STRONG> | <STRONG>C3</STRONG> |
253 +-----+------+-------+
254 A few of these predefined values do <EM>not</EM> correspond to a real key:
256 <STRONG>o</STRONG> <STRONG>KEY_RESIZE</STRONG> is returned when the <STRONG>SIGWINCH</STRONG> signal has been detected
257 (see <STRONG><A HREF="curs_initscr.3x.html">initscr(3x)</A></STRONG> and <STRONG><A HREF="resizeterm.3x.html">resizeterm(3x)</A></STRONG>). This code is returned
258 whether or not <STRONG>keypad</STRONG> has been enabled.
260 <STRONG>o</STRONG> <STRONG>KEY_MOUSE</STRONG> is returned for mouse-events (see <STRONG><A HREF="curs_mouse.3x.html">curs_mouse(3x)</A></STRONG>). This
261 code relies upon whether or not <STRONG><A HREF="curs_inopts.3x.html">keypad(3x)</A></STRONG> has been enabled, be-
262 cause (e.g., with <EM>xterm</EM> mouse prototocol) ncurses must read escape
263 sequences, just like a function key.
266 </PRE><H3><a name="h3-Testing-key-codes">Testing key-codes</a></H3><PRE>
267 The <STRONG>has_key</STRONG> routine takes a key-code value from the above list, and re-
268 turns <STRONG>TRUE</STRONG> or <STRONG>FALSE</STRONG> according to whether the current terminal type rec-
269 ognizes a key with that value.
271 The library also supports these extensions:
273 <STRONG>define_key</STRONG>
274 defines a key-code for a given string (see <STRONG><A HREF="define_key.3x.html">define_key(3x)</A></STRONG>).
276 <STRONG>key_defined</STRONG>
277 checks if there is a key-code defined for a given string (see
278 <STRONG><A HREF="key_defined.3x.html">key_defined(3x)</A></STRONG>).
281 </PRE><H2><a name="h2-RETURN-VALUE">RETURN VALUE</a></H2><PRE>
282 All routines return the integer <STRONG>ERR</STRONG> upon failure and an integer value
283 other than <STRONG>ERR</STRONG> (<STRONG>OK</STRONG> in the case of <STRONG>ungetch</STRONG>) upon successful completion.
285 <STRONG>ungetch</STRONG>
286 returns <STRONG>ERR</STRONG> if there is no more room in the FIFO.
288 <STRONG>wgetch</STRONG>
289 returns <STRONG>ERR</STRONG> if the window pointer is null, or if its timeout
290 expires without having any data, or if the execution was inter-
291 rupted by a signal (<STRONG>errno</STRONG> will be set to <STRONG>EINTR</STRONG>).
293 Functions with a "mv" prefix first perform a cursor movement using
294 <STRONG>wmove</STRONG>, and return an error if the position is outside the window, or if
295 the window pointer is null.
298 </PRE><H2><a name="h2-NOTES">NOTES</a></H2><PRE>
299 Use of the escape key by a programmer for a single character function
300 is discouraged, as it will cause a delay of up to one second while the
301 keypad code looks for a following function-key sequence.
303 Some keys may be the same as commonly used control keys, e.g., <STRONG>KEY_EN-</STRONG>
304 <STRONG>TER</STRONG> versus control/M, <STRONG>KEY_BACKSPACE</STRONG> versus control/H. Some curses im-
305 plementations may differ according to whether they treat these control
306 keys specially (and ignore the terminfo), or use the terminfo defini-
307 tions. <STRONG>Ncurses</STRONG> uses the terminfo definition. If it says that <STRONG>KEY_EN-</STRONG>
308 <STRONG>TER</STRONG> is control/M, <STRONG>getch</STRONG> will return <STRONG>KEY_ENTER</STRONG> when you press control/M.
310 Generally, <STRONG>KEY_ENTER</STRONG> denotes the character(s) sent by the <EM>Enter</EM> key on
313 <STRONG>o</STRONG> the terminal description lists the most useful keys,
315 <STRONG>o</STRONG> the <EM>Enter</EM> key on the regular keyboard is already handled by the
316 standard ASCII characters for carriage-return and line-feed,
318 <STRONG>o</STRONG> depending on whether <STRONG>nl</STRONG> or <STRONG>nonl</STRONG> was called, pressing "Enter" on the
319 regular keyboard may return either a carriage-return or line-feed,
322 <STRONG>o</STRONG> "Enter or send" is the standard description for this key.
324 When using <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>, or <STRONG>mvwgetch</STRONG>, nocbreak mode
325 (<STRONG>nocbreak</STRONG>) and echo mode (<STRONG>echo</STRONG>) should not be used at the same time.
326 Depending on the state of the tty driver when each character is typed,
327 the program may produce undesirable results.
329 Note that <STRONG>getch</STRONG>, <STRONG>mvgetch</STRONG>, and <STRONG>mvwgetch</STRONG> may be macros.
331 Historically, the set of keypad macros was largely defined by the ex-
332 tremely function-key-rich keyboard of the AT&T 7300, aka 3B1, aka Sa-
333 fari 4. Modern personal computers usually have only a small subset of
334 these. IBM PC-style consoles typically support little more than
335 <STRONG>KEY_UP</STRONG>, <STRONG>KEY_DOWN</STRONG>, <STRONG>KEY_LEFT</STRONG>, <STRONG>KEY_RIGHT</STRONG>, <STRONG>KEY_HOME</STRONG>, <STRONG>KEY_END</STRONG>, <STRONG>KEY_NPAGE</STRONG>,
336 <STRONG>KEY_PPAGE</STRONG>, and function keys 1 through 12. The Ins key is usually
337 mapped to <STRONG>KEY_IC</STRONG>.
340 </PRE><H2><a name="h2-PORTABILITY">PORTABILITY</a></H2><PRE>
341 The *get* functions are described in the XSI Curses standard, Issue 4.
342 They read single-byte characters only. The standard specifies that
343 they return <STRONG>ERR</STRONG> on failure, but specifies no error conditions.
345 The echo behavior of these functions on input of <STRONG>KEY_</STRONG> or backspace
346 characters was not specified in the SVr4 documentation. This descrip-
347 tion is adopted from the XSI Curses standard.
349 The behavior of <STRONG>getch</STRONG> and friends in the presence of handled signals is
350 unspecified in the SVr4 and XSI Curses documentation. Under historical
351 curses implementations, it varied depending on whether the operating
352 system's implementation of handled signal receipt interrupts a <STRONG>read(2)</STRONG>
353 call in progress or not, and also (in some implementations) depending
354 on whether an input timeout or non-blocking mode has been set.
356 <STRONG>KEY_MOUSE</STRONG> is mentioned in XSI Curses, along with a few related terminfo
357 capabilities, but no higher-level functions use the feature. The im-
358 plementation in ncurses is an extension.
360 <STRONG>KEY_RESIZE</STRONG> is an extension first implemented for ncurses. NetBSD curs-
361 es later added this extension.
363 Programmers concerned about portability should be prepared for either
364 of two cases: (a) signal receipt does not interrupt <STRONG>getch</STRONG>; (b) signal
365 receipt interrupts <STRONG>getch</STRONG> and causes it to return <STRONG>ERR</STRONG> with <STRONG>errno</STRONG> set to
366 <STRONG>EINTR</STRONG>.
368 The <STRONG>has_key</STRONG> function is unique to <STRONG>ncurses</STRONG>. We recommend that any code
369 using it be conditionalized on the <STRONG>NCURSES_VERSION</STRONG> feature macro.
372 </PRE><H2><a name="h2-SEE-ALSO">SEE ALSO</a></H2><PRE>
373 <STRONG><A HREF="ncurses.3x.html">curses(3x)</A></STRONG>, <STRONG><A HREF="curs_inopts.3x.html">curs_inopts(3x)</A></STRONG>, <STRONG><A HREF="curs_outopts.3x.html">curs_outopts(3x)</A></STRONG>, <STRONG><A HREF="curs_mouse.3x.html">curs_mouse(3x)</A></STRONG>,
374 <STRONG><A HREF="curs_move.3x.html">curs_move(3x)</A></STRONG>, <STRONG><A HREF="curs_refresh.3x.html">curs_refresh(3x)</A></STRONG>, <STRONG><A HREF="curs_variables.3x.html">curs_variables(3x)</A></STRONG>, <STRONG><A HREF="resizeterm.3x.html">resizeterm(3x)</A></STRONG>.
376 Comparable functions in the wide-character (ncursesw) library are de-
377 scribed in <STRONG><A HREF="curs_get_wch.3x.html">curs_get_wch(3x)</A></STRONG>.
381 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>
385 <li><a href="#h2-NAME">NAME</a></li>
386 <li><a href="#h2-SYNOPSIS">SYNOPSIS</a></li>
387 <li><a href="#h2-DESCRIPTION">DESCRIPTION</a>
389 <li><a href="#h3-Reading-characters">Reading characters</a></li>
390 <li><a href="#h3-Keypad-mode">Keypad mode</a></li>
391 <li><a href="#h3-Ungetting-characters">Ungetting characters</a></li>
392 <li><a href="#h3-Predefined-key-codes">Predefined key-codes</a></li>
393 <li><a href="#h3-Testing-key-codes">Testing key-codes</a></li>
396 <li><a href="#h2-RETURN-VALUE">RETURN VALUE</a></li>
397 <li><a href="#h2-NOTES">NOTES</a></li>
398 <li><a href="#h2-PORTABILITY">PORTABILITY</a></li>
399 <li><a href="#h2-SEE-ALSO">SEE ALSO</a></li>