3 ****************************************************************************
4 * Copyright 2018-2022,2023 Thomas E. Dickey *
5 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
7 * Permission is hereby granted, free of charge, to any person obtaining a *
8 * copy of this software and associated documentation files (the *
9 * "Software"), to deal in the Software without restriction, including *
10 * without limitation the rights to use, copy, modify, merge, publish, *
11 * distribute, distribute with modifications, sublicense, and/or sell *
12 * copies of the Software, and to permit persons to whom the Software is *
13 * furnished to do so, subject to the following conditions: *
15 * The above copyright notice and this permission notice shall be included *
16 * in all copies or substantial portions of the Software. *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
26 * Except as contained in this notice, the name(s) of the above copyright *
27 * holders shall not be used in advertising or otherwise to promote the *
28 * sale, use or other dealings in this Software without prior written *
30 ****************************************************************************
31 * @Id: curs_getch.3x,v 1.72 2023/09/16 23:34:43 tom Exp @
33 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
36 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
37 <meta name="generator" content="Manpage converted by man2html - see https://invisible-island.net/scripts/readme.html#others_scripts">
38 <TITLE>curs_getch 3x 2023-09-16 ncurses 6.4 Library calls</TITLE>
39 <link rel="author" href="mailto:bug-ncurses@gnu.org">
43 <H1 class="no-header">curs_getch 3x 2023-09-16 ncurses 6.4 Library calls</H1>
45 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG> Library calls <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>
50 </PRE><H2><a name="h2-NAME">NAME</a></H2><PRE>
51 <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>, <STRONG>mvwgetch</STRONG>, <STRONG>ungetch</STRONG>, <STRONG>has_key</STRONG> - get (or push back)
52 characters from <EM>curses</EM> terminal keyboard
55 </PRE><H2><a name="h2-SYNOPSIS">SYNOPSIS</a></H2><PRE>
56 <STRONG>#include</STRONG> <STRONG><curses.h></STRONG>
58 <STRONG>int</STRONG> <STRONG>getch(void);</STRONG>
59 <STRONG>int</STRONG> <STRONG>wgetch(WINDOW</STRONG> <STRONG>*</STRONG><EM>win</EM><STRONG>);</STRONG>
61 <STRONG>int</STRONG> <STRONG>mvgetch(int</STRONG> <EM>y</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>x</EM><STRONG>);</STRONG>
62 <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>
64 <STRONG>int</STRONG> <STRONG>ungetch(int</STRONG> <EM>ch</EM><STRONG>);</STRONG>
67 <STRONG>int</STRONG> <STRONG>has_key(int</STRONG> <EM>ch</EM><STRONG>);</STRONG>
70 </PRE><H2><a name="h2-DESCRIPTION">DESCRIPTION</a></H2><PRE>
72 </PRE><H3><a name="h3-Reading-characters">Reading characters</a></H3><PRE>
73 The <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG> and <STRONG>mvwgetch</STRONG>, routines read a character from
74 the window. In no-delay mode, if no input is waiting, the value <STRONG>ERR</STRONG> is
75 returned. In delay mode, the program waits until the system passes
76 text through to the program. Depending on the setting of <STRONG>cbreak</STRONG>, this
77 is after one character (cbreak mode), or after the first newline
78 (nocbreak mode). In half-delay mode, the program waits until a
79 character is typed or the specified timeout has been reached.
81 If <STRONG>echo</STRONG> is enabled, and the window is not a pad, then the character
82 will also be echoed into the designated window according to the
85 <STRONG>o</STRONG> If the character is the current erase character, left arrow, or
86 backspace, the cursor is moved one space to the left and that
87 screen position is erased as if <STRONG>delch</STRONG> had been called.
89 <STRONG>o</STRONG> If the character value is any other <STRONG>KEY_</STRONG> define, the user is
90 alerted with a <STRONG>beep</STRONG> call.
92 <STRONG>o</STRONG> If the character is a carriage-return, and if <STRONG>nl</STRONG> is enabled, it is
93 translated to a line-feed after echoing.
95 <STRONG>o</STRONG> Otherwise the character is simply output to the screen.
97 If the window is not a pad, and it has been moved or modified since the
98 last call to <STRONG>wrefresh</STRONG>, <STRONG>wrefresh</STRONG> will be called before another character
102 </PRE><H3><a name="h3-Keypad-mode">Keypad mode</a></H3><PRE>
103 If <STRONG>keypad</STRONG> is <STRONG>TRUE</STRONG>, and a function key is pressed, the token for that
104 function key is returned instead of the raw characters:
106 <STRONG>o</STRONG> The predefined function keys are listed in <STRONG><curses.h></STRONG> as macros
107 with values outside the range of 8-bit characters. Their names
108 begin with <STRONG>KEY_</STRONG>.
110 <STRONG>o</STRONG> Other (user-defined) function keys which may be defined using
111 <STRONG><A HREF="define_key.3x.html">define_key(3x)</A></STRONG> have no names, but also are expected to have values
112 outside the range of 8-bit characters.
114 Thus, a variable intended to hold the return value of a function key
115 must be of short size or larger.
117 When a character that could be the beginning of a function key is
118 received (which, on modern terminals, means an escape character),
119 <STRONG>curses</STRONG> sets a timer. If the remainder of the sequence does not come in
120 within the designated time, the character is passed through; otherwise,
121 the function key value is returned. For this reason, many terminals
122 experience a delay between the time a user presses the escape key and
123 the escape is returned to the program.
125 In <STRONG>ncurses</STRONG>, the timer normally expires after the value in <STRONG>ESCDELAY</STRONG> (see
126 <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;
127 it is an infinite (or very large) value. Because function keys usually
128 begin with an escape character, the terminal may appear to hang in
129 notimeout mode after pressing the escape key until another key is
133 </PRE><H3><a name="h3-Ungetting-characters">Ungetting characters</a></H3><PRE>
134 The <STRONG>ungetch</STRONG> routine places <EM>ch</EM> back onto the input queue to be returned
135 by the next call to <STRONG>wgetch</STRONG>. There is just one input queue for all
139 </PRE><H3><a name="h3-Predefined-key-codes">Predefined key-codes</a></H3><PRE>
140 The following special keys are defined in <STRONG><curses.h></STRONG>.
142 <STRONG>o</STRONG> Except for the special case <STRONG>KEY_RESIZE</STRONG>, it is necessary to enable
143 <STRONG>keypad</STRONG> for <STRONG>getch</STRONG> to return these codes.
145 <STRONG>o</STRONG> Not all of these are necessarily supported on any particular
148 <STRONG>o</STRONG> The naming convention may seem obscure, with some apparent
149 misspellings (such as "RSUME" for "resume"). The names correspond
150 to the long terminfo capability names for the keys, and were
151 defined long ago, in the 1980s.
153 <STRONG>Name</STRONG> <STRONG>Key</STRONG> <STRONG>name</STRONG>
154 -------------------------------------------------
156 KEY_DOWN The four arrow keys ...
160 KEY_HOME Home key (upward+left arrow)
161 KEY_BACKSPACE Backspace
162 KEY_F0 Function keys; space for 64 keys
164 KEY_F(<EM>n</EM>) For 0 <= <EM>n</EM> <= 63
167 KEY_DC Delete character
168 KEY_IC Insert char or enter insert mode
169 KEY_EIC Exit insert char mode
170 KEY_CLEAR Clear screen
171 KEY_EOS Clear to end of screen
172 KEY_EOL Clear to end of line
173 KEY_SF Scroll 1 line forward
174 KEY_SR Scroll 1 line backward (reverse)
176 KEY_PPAGE Previous page
179 KEY_CATAB Clear all tabs
180 KEY_ENTER Enter or send
181 KEY_SRESET Soft (partial) reset
182 KEY_RESET Reset or hard reset
184 KEY_PRINT Print or copy
185 KEY_LL Home down or bottom (lower left)
186 KEY_A1 Upper left of keypad
187 KEY_A3 Upper right of keypad
188 KEY_B2 Center of keypad
189 KEY_C1 Lower left of keypad
190 KEY_C3 Lower right of keypad
191 KEY_BTAB Back tab key
192 KEY_BEG Beg(inning) key
193 KEY_CANCEL Cancel key
195 KEY_COMMAND Cmd (command) key
197 KEY_CREATE Create key
203 KEY_MESSAGE Message key
204 KEY_MOUSE Mouse event occurred
206 KEY_NEXT Next object key
208 KEY_OPTIONS Options key
209 KEY_PREVIOUS Previous object key
211 KEY_REFERENCE Ref(erence) key
212 KEY_REFRESH Refresh key
213 KEY_REPLACE Replace key
214 KEY_RESIZE Screen resized
215 KEY_RESTART Restart key
216 KEY_RESUME Resume key
218 KEY_SBEG Shifted beginning key
219 KEY_SCANCEL Shifted cancel key
220 KEY_SCOMMAND Shifted command key
221 KEY_SCOPY Shifted copy key
222 KEY_SCREATE Shifted create key
223 KEY_SDC Shifted delete char key
224 KEY_SDL Shifted delete line key
225 KEY_SELECT Select key
226 KEY_SEND Shifted end key
227 KEY_SEOL Shifted clear line key
228 KEY_SEXIT Shifted exit key
229 KEY_SFIND Shifted find key
230 KEY_SHELP Shifted help key
231 KEY_SHOME Shifted home key
232 KEY_SIC Shifted insert key
233 KEY_SLEFT Shifted left arrow key
234 KEY_SMESSAGE Shifted message key
235 KEY_SMOVE Shifted move key
236 KEY_SNEXT Shifted next key
237 KEY_SOPTIONS Shifted options key
238 KEY_SPREVIOUS Shifted prev key
239 KEY_SPRINT Shifted print key
240 KEY_SREDO Shifted redo key
241 KEY_SREPLACE Shifted replace key
242 KEY_SRIGHT Shifted right arrow key
243 KEY_SRSUME Shifted resume key
244 KEY_SSAVE Shifted save key
245 KEY_SSUSPEND Shifted suspend key
246 KEY_SUNDO Shifted undo key
247 KEY_SUSPEND Suspend key
250 Keypad is arranged like this:
252 +-----+------+-------+
253 | <STRONG>A1</STRONG> | <STRONG>up</STRONG> | <STRONG>A3</STRONG> |
254 +-----+------+-------+
255 |<STRONG>left</STRONG> | <STRONG>B2</STRONG> | <STRONG>right</STRONG> |
256 +-----+------+-------+
257 | <STRONG>C1</STRONG> | <STRONG>down</STRONG> | <STRONG>C3</STRONG> |
258 +-----+------+-------+
259 A few of these predefined values do <EM>not</EM> correspond to a real key:
261 <STRONG>o</STRONG> <STRONG>KEY_RESIZE</STRONG> is returned when the <STRONG>SIGWINCH</STRONG> signal has been detected
262 (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
263 whether or not <STRONG>keypad</STRONG> has been enabled.
265 <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
266 code relies upon whether or not <STRONG><A HREF="curs_inopts.3x.html">keypad(3x)</A></STRONG> has been enabled,
267 because (e.g., with <STRONG>xterm(1)</STRONG> mouse prototocol) ncurses must read
268 escape sequences, just like a function key.
271 </PRE><H3><a name="h3-Testing-key-codes">Testing key-codes</a></H3><PRE>
272 The <STRONG>has_key</STRONG> routine takes a key-code value from the above list, and
273 returns <STRONG>TRUE</STRONG> or <STRONG>FALSE</STRONG> according to whether the current terminal type
274 recognizes a key with that value.
276 The library also supports these extensions:
278 <STRONG>define_key</STRONG>
279 defines a key-code for a given string (see <STRONG><A HREF="define_key.3x.html">define_key(3x)</A></STRONG>).
281 <STRONG>key_defined</STRONG>
282 checks if there is a key-code defined for a given string (see
283 <STRONG><A HREF="key_defined.3x.html">key_defined(3x)</A></STRONG>).
286 </PRE><H2><a name="h2-RETURN-VALUE">RETURN VALUE</a></H2><PRE>
287 All routines return the integer <STRONG>ERR</STRONG> upon failure and an integer value
288 other than <STRONG>ERR</STRONG> (<STRONG>OK</STRONG> in the case of <STRONG>ungetch</STRONG>) upon successful completion.
290 <STRONG>ungetch</STRONG>
291 returns <STRONG>ERR</STRONG> if there is no more room in the FIFO.
293 <STRONG>wgetch</STRONG>
294 returns <STRONG>ERR</STRONG> if the window pointer is null, or if its timeout
295 expires without having any data, or if the execution was
296 interrupted by a signal (<STRONG>errno</STRONG> will be set to <STRONG>EINTR</STRONG>).
298 Functions with a "mv" prefix first perform a cursor movement using
299 <STRONG>wmove</STRONG>, and return an error if the position is outside the window, or if
300 the window pointer is null.
303 </PRE><H2><a name="h2-NOTES">NOTES</a></H2><PRE>
304 Use of the escape key by a programmer for a single character function
305 is discouraged, as it will cause a delay of up to one second while the
306 keypad code looks for a following function-key sequence.
308 Some keys may be the same as commonly used control keys, e.g.,
309 <STRONG>KEY_ENTER</STRONG> versus control/M, <STRONG>KEY_BACKSPACE</STRONG> versus control/H. Some
310 curses implementations may differ according to whether they treat these
311 control keys specially (and ignore the terminfo), or use the terminfo
312 definitions. <STRONG>Ncurses</STRONG> uses the terminfo definition. If it says that
313 <STRONG>KEY_ENTER</STRONG> is control/M, <STRONG>getch</STRONG> will return <STRONG>KEY_ENTER</STRONG> when you press
316 Generally, <STRONG>KEY_ENTER</STRONG> denotes the character(s) sent by the <EM>Enter</EM> key on
319 <STRONG>o</STRONG> the terminal description lists the most useful keys,
321 <STRONG>o</STRONG> the <EM>Enter</EM> key on the regular keyboard is already handled by the
322 standard ASCII characters for carriage-return and line-feed,
324 <STRONG>o</STRONG> depending on whether <STRONG>nl</STRONG> or <STRONG>nonl</STRONG> was called, pressing "Enter" on the
325 regular keyboard may return either a carriage-return or line-feed,
328 <STRONG>o</STRONG> "Enter or send" is the standard description for this key.
330 When using <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>, or <STRONG>mvwgetch</STRONG>, nocbreak mode
331 (<STRONG>nocbreak</STRONG>) and echo mode (<STRONG>echo</STRONG>) should not be used at the same time.
332 Depending on the state of the tty driver when each character is typed,
333 the program may produce undesirable results.
335 Note that <STRONG>getch</STRONG>, <STRONG>mvgetch</STRONG>, and <STRONG>mvwgetch</STRONG> may be macros.
337 Historically, the set of keypad macros was largely defined by the
338 extremely function-key-rich keyboard of the AT&T 7300, aka 3B1, aka
339 Safari 4. Modern personal computers usually have only a small subset
340 of these. IBM PC-style consoles typically support little more than
341 <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>,
342 <STRONG>KEY_PPAGE</STRONG>, and function keys 1 through 12. The Ins key is usually
343 mapped to <STRONG>KEY_IC</STRONG>.
346 </PRE><H2><a name="h2-PORTABILITY">PORTABILITY</a></H2><PRE>
347 The *get* functions are described in the XSI Curses standard, Issue 4.
348 They read single-byte characters only. The standard specifies that
349 they return <STRONG>ERR</STRONG> on failure, but specifies no error conditions.
351 The echo behavior of these functions on input of <STRONG>KEY_</STRONG> or backspace
352 characters was not specified in the SVr4 documentation. This
353 description is adopted from the XSI Curses standard.
355 The behavior of <STRONG>getch</STRONG> and friends in the presence of handled signals is
356 unspecified in the SVr4 and XSI Curses documentation. Under historical
357 curses implementations, it varied depending on whether the operating
358 system's implementation of handled signal receipt interrupts a <STRONG>read(2)</STRONG>
359 call in progress or not, and also (in some implementations) depending
360 on whether an input timeout or non-blocking mode has been set.
362 <STRONG>KEY_MOUSE</STRONG> is mentioned in XSI Curses, along with a few related terminfo
363 capabilities, but no higher-level functions use the feature. The
364 implementation in ncurses is an extension.
366 <STRONG>KEY_RESIZE</STRONG> is an extension first implemented for ncurses. NetBSD
367 curses later added this extension.
369 Programmers concerned about portability should be prepared for either
370 of two cases: (a) signal receipt does not interrupt <STRONG>getch</STRONG>; (b) signal
371 receipt interrupts <STRONG>getch</STRONG> and causes it to return <STRONG>ERR</STRONG> with <STRONG>errno</STRONG> set to
372 <STRONG>EINTR</STRONG>.
374 The <STRONG>has_key</STRONG> function is unique to <STRONG>ncurses</STRONG>. We recommend that any code
375 using it be conditionalized on the <STRONG>NCURSES_VERSION</STRONG> feature macro.
378 </PRE><H2><a name="h2-SEE-ALSO">SEE ALSO</a></H2><PRE>
379 <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_mouse.3x.html">curs_mouse(3x)</A></STRONG>, <STRONG><A HREF="curs_move.3x.html">curs_move(3x)</A></STRONG>,
380 <STRONG><A HREF="curs_outopts.3x.html">curs_outopts(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>.
382 Comparable functions in the wide-character (ncursesw) library are
383 described in <STRONG><A HREF="curs_get_wch.3x.html">curs_get_wch(3x)</A></STRONG>.
387 ncurses 6.4 2023-09-16 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>
391 <li><a href="#h2-NAME">NAME</a></li>
392 <li><a href="#h2-SYNOPSIS">SYNOPSIS</a></li>
393 <li><a href="#h2-DESCRIPTION">DESCRIPTION</a>
395 <li><a href="#h3-Reading-characters">Reading characters</a></li>
396 <li><a href="#h3-Keypad-mode">Keypad mode</a></li>
397 <li><a href="#h3-Ungetting-characters">Ungetting characters</a></li>
398 <li><a href="#h3-Predefined-key-codes">Predefined key-codes</a></li>
399 <li><a href="#h3-Testing-key-codes">Testing key-codes</a></li>
402 <li><a href="#h2-RETURN-VALUE">RETURN VALUE</a></li>
403 <li><a href="#h2-NOTES">NOTES</a></li>
404 <li><a href="#h2-PORTABILITY">PORTABILITY</a></li>
405 <li><a href="#h2-SEE-ALSO">SEE ALSO</a></li>