3 ****************************************************************************
4 * Copyright 2018-2023,2024 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.78 2024/02/17 19:27:03 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 2024-02-17 ncurses 6.4 Library calls</TITLE>
39 <link rel="author" href="mailto:bug-ncurses@gnu.org">
43 <H1 class="no-header">curs_getch 3x 2024-02-17 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>
60 <STRONG>int</STRONG> <STRONG>mvgetch(int</STRONG> <EM>y</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>x</EM><STRONG>);</STRONG>
61 <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>
63 <STRONG>int</STRONG> <STRONG>ungetch(int</STRONG> <EM>ch</EM><STRONG>);</STRONG>
65 <EM>/*</EM> <EM>extension</EM> <EM>*/</EM>
66 <STRONG>int</STRONG> <STRONG>has_key(int</STRONG> <EM>ch</EM><STRONG>);</STRONG>
69 </PRE><H2><a name="h2-DESCRIPTION">DESCRIPTION</a></H2><PRE>
71 </PRE><H3><a name="h3-Reading-Characters">Reading Characters</a></H3><PRE>
72 The <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG> and <STRONG>mvwgetch</STRONG>, routines read a character from
73 the window. In no-delay mode, if no input is waiting, the value <STRONG>ERR</STRONG> is
74 returned. In delay mode, the program waits until the system passes
75 text through to the program. Depending on the setting of <STRONG>cbreak</STRONG>, this
76 is after one character (cbreak mode), or after the first newline
77 (nocbreak mode). In half-delay mode, the program waits until a
78 character is typed or the specified timeout has been reached.
80 If <STRONG>echo</STRONG> is enabled, and the window is not a pad, then the character
81 will also be echoed into the designated window according to the
84 <STRONG>o</STRONG> If the character is the current erase character, left arrow, or
85 backspace, the cursor is moved one space to the left and that
86 screen position is erased as if <STRONG>delch</STRONG> had been called.
88 <STRONG>o</STRONG> If the character value is any other <STRONG>KEY_</STRONG> define, the user is
89 alerted with a <STRONG>beep</STRONG> call.
91 <STRONG>o</STRONG> If the character is a carriage-return, and if <STRONG>nl</STRONG> is enabled, it is
92 translated to a line-feed after echoing.
94 <STRONG>o</STRONG> Otherwise the character is simply output to the screen.
96 If the window is not a pad, and it has been moved or modified since the
97 last call to <STRONG>wrefresh</STRONG>, <STRONG>wrefresh</STRONG> will be called before another character
101 </PRE><H3><a name="h3-Keypad-Mode">Keypad Mode</a></H3><PRE>
102 If <STRONG>keypad</STRONG> is <STRONG>TRUE</STRONG>, and a function key is pressed, the token for that
103 function key is returned instead of the raw characters:
105 <STRONG>o</STRONG> The predefined function keys are listed in <STRONG><curses.h></STRONG> as macros
106 with values outside the range of 8-bit characters. Their names
107 begin with <STRONG>KEY_</STRONG>.
109 <STRONG>o</STRONG> Other (user-defined) function keys which may be defined using
110 <STRONG><A HREF="define_key.3x.html">define_key(3x)</A></STRONG> have no names, but also are expected to have values
111 outside the range of 8-bit characters.
113 Thus, a variable intended to hold the return value of a function key
114 must be of short size or larger.
116 When a character that could be the beginning of a function key is
117 received (which, on modern terminals, means an escape character),
118 <STRONG>curses</STRONG> sets a timer. If the remainder of the sequence does not come in
119 within the designated time, the character is passed through; otherwise,
120 the function key value is returned. For this reason, many terminals
121 experience a delay between the time a user presses the escape key and
122 the escape is returned to the program.
124 In <EM>ncurses</EM>, the timer normally expires after the value in <STRONG>ESCDELAY</STRONG> (see
125 <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;
126 it is an infinite (or very large) value. Because function keys usually
127 begin with an escape character, the terminal may appear to hang in
128 notimeout mode after pressing the escape key until another key is
132 </PRE><H3><a name="h3-Ungetting-Characters">Ungetting Characters</a></H3><PRE>
133 The <STRONG>ungetch</STRONG> routine places <EM>ch</EM> back onto the input queue to be returned
134 by the next call to <STRONG>wgetch</STRONG>. There is just one input queue for all
138 </PRE><H3><a name="h3-Predefined-Key-Codes">Predefined Key Codes</a></H3><PRE>
139 The following special keys are defined in <STRONG><curses.h></STRONG>.
141 <STRONG>o</STRONG> Except for the special case <STRONG>KEY_RESIZE</STRONG>, it is necessary to enable
142 <STRONG>keypad</STRONG> for <STRONG>getch</STRONG> to return these codes.
144 <STRONG>o</STRONG> Not all of these are necessarily supported on any particular
147 <STRONG>o</STRONG> The naming convention may seem obscure, with some apparent
148 misspellings (such as "RSUME" for "resume"). The names correspond
149 to the long terminfo capability names for the keys, and were
150 defined long ago, in the 1980s.
152 <STRONG>Name</STRONG> <STRONG>Key</STRONG> <STRONG>name</STRONG>
153 -------------------------------------------------
155 KEY_DOWN The four arrow keys ...
159 KEY_HOME Home key (upward+left arrow)
160 KEY_BACKSPACE Backspace
161 KEY_F0 Function keys; space for 64 keys
163 KEY_F(<EM>n</EM>) For 0 <= <EM>n</EM> <= 63
166 KEY_DC Delete character
167 KEY_IC Insert char or enter insert mode
168 KEY_EIC Exit insert char mode
169 KEY_CLEAR Clear screen
170 KEY_EOS Clear to end of screen
171 KEY_EOL Clear to end of line
172 KEY_SF Scroll 1 line forward
173 KEY_SR Scroll 1 line backward (reverse)
175 KEY_PPAGE Previous page
178 KEY_CATAB Clear all tabs
179 KEY_ENTER Enter or send
180 KEY_SRESET Soft (partial) reset
181 KEY_RESET Reset or hard reset
182 KEY_PRINT Print or copy
184 KEY_LL Home down or bottom (lower left)
185 KEY_A1 Upper left of keypad
186 KEY_A3 Upper right of keypad
187 KEY_B2 Center of keypad
188 KEY_C1 Lower left of keypad
189 KEY_C3 Lower right of keypad
190 KEY_BTAB Back tab key
191 KEY_BEG Beg(inning) key
192 KEY_CANCEL Cancel key
194 KEY_COMMAND Cmd (command) key
196 KEY_CREATE Create key
202 KEY_MESSAGE Message key
203 KEY_MOUSE Mouse event occurred
205 KEY_NEXT Next object key
207 KEY_OPTIONS Options key
208 KEY_PREVIOUS Previous object key
210 KEY_REFERENCE Ref(erence) key
211 KEY_REFRESH Refresh key
212 KEY_REPLACE Replace key
213 KEY_RESIZE Screen resized
214 KEY_RESTART Restart key
215 KEY_RESUME Resume key
217 KEY_SBEG Shifted beginning key
218 KEY_SCANCEL Shifted cancel key
219 KEY_SCOMMAND Shifted command key
220 KEY_SCOPY Shifted copy key
221 KEY_SCREATE Shifted create key
222 KEY_SDC Shifted delete char key
223 KEY_SDL Shifted delete line key
224 KEY_SELECT Select key
225 KEY_SEND Shifted end key
226 KEY_SEOL Shifted clear line key
227 KEY_SEXIT Shifted exit key
228 KEY_SFIND Shifted find key
229 KEY_SHELP Shifted help key
230 KEY_SHOME Shifted home key
231 KEY_SIC Shifted insert key
232 KEY_SLEFT Shifted left arrow key
233 KEY_SMESSAGE Shifted message key
234 KEY_SMOVE Shifted move key
235 KEY_SNEXT Shifted next key
236 KEY_SOPTIONS Shifted options key
237 KEY_SPREVIOUS Shifted prev key
238 KEY_SPRINT Shifted print key
239 KEY_SREDO Shifted redo key
240 KEY_SREPLACE Shifted replace key
241 KEY_SRIGHT Shifted right arrow key
242 KEY_SRSUME Shifted resume key
243 KEY_SSAVE Shifted save key
244 KEY_SSUSPEND Shifted suspend key
245 KEY_SUNDO Shifted undo key
246 KEY_SUSPEND Suspend key
249 Keypad is arranged like this:
251 +-----+------+-------+
252 | <STRONG>A1</STRONG> | <STRONG>up</STRONG> | <STRONG>A3</STRONG> |
253 +-----+------+-------+
254 |<STRONG>left</STRONG> | <STRONG>B2</STRONG> | <STRONG>right</STRONG> |
255 +-----+------+-------+
256 | <STRONG>C1</STRONG> | <STRONG>down</STRONG> | <STRONG>C3</STRONG> |
257 +-----+------+-------+
258 A few of these predefined values do <EM>not</EM> correspond to a real key:
260 <STRONG>o</STRONG> <STRONG>KEY_RESIZE</STRONG> is returned when the <STRONG>SIGWINCH</STRONG> signal has been detected
261 (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
262 whether or not <STRONG>keypad</STRONG> has been enabled.
264 <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
265 code relies upon whether or not <STRONG><A HREF="curs_inopts.3x.html">keypad(3x)</A></STRONG> has been enabled,
266 because (e.g., with <STRONG>xterm(1)</STRONG> mouse prototocol) <EM>ncurses</EM> must read
267 escape sequences, just like a function key.
270 </PRE><H3><a name="h3-Testing-Key-Codes">Testing Key Codes</a></H3><PRE>
271 The <STRONG>has_key</STRONG> routine takes a key-code value from the above list, and
272 returns <STRONG>TRUE</STRONG> or <STRONG>FALSE</STRONG> according to whether the current terminal type
273 recognizes a key with that value.
275 The library also supports these extensions:
277 <STRONG>define_key</STRONG>
278 defines a key-code for a given string (see <STRONG><A HREF="define_key.3x.html">define_key(3x)</A></STRONG>).
280 <STRONG>key_defined</STRONG>
281 checks if there is a key-code defined for a given string (see
282 <STRONG><A HREF="key_defined.3x.html">key_defined(3x)</A></STRONG>).
285 </PRE><H2><a name="h2-RETURN-VALUE">RETURN VALUE</a></H2><PRE>
286 All routines return the integer <STRONG>ERR</STRONG> upon failure and an integer value
287 other than <STRONG>ERR</STRONG> (<STRONG>OK</STRONG> in the case of <STRONG>ungetch</STRONG>) upon successful completion.
289 <STRONG>ungetch</STRONG>
290 returns <STRONG>ERR</STRONG> if there is no more room in the FIFO.
292 <STRONG>wgetch</STRONG>
293 returns <STRONG>ERR</STRONG>
295 <STRONG>o</STRONG> if the window pointer is null, or
297 <STRONG>o</STRONG> if its timeout expires without having any data, or
299 <STRONG>o</STRONG> if the execution was interrupted by a signal (<STRONG>errno</STRONG> will be
300 set to <STRONG>EINTR</STRONG>).
302 Functions with a "mv" prefix first perform a cursor movement using
303 <STRONG>wmove</STRONG>, and return an error if the position is outside the window, or if
304 the window pointer is null.
307 </PRE><H2><a name="h2-NOTES">NOTES</a></H2><PRE>
308 Use of the escape key by a programmer for a single character function
309 is discouraged, as it will cause a delay of up to one second while the
310 keypad code looks for a following function-key sequence.
312 Some keys may be the same as commonly used control keys, e.g.,
313 <STRONG>KEY_ENTER</STRONG> versus control/M, <STRONG>KEY_BACKSPACE</STRONG> versus control/H. Some
314 curses implementations may differ according to whether they treat these
315 control keys specially (and ignore the terminfo), or use the terminfo
316 definitions. <EM>ncurses</EM> uses the terminfo definition. If it says that
317 <STRONG>KEY_ENTER</STRONG> is control/M, <STRONG>getch</STRONG> will return <STRONG>KEY_ENTER</STRONG> when you press
320 Generally, <STRONG>KEY_ENTER</STRONG> denotes the character(s) sent by the <EM>Enter</EM> key on
323 <STRONG>o</STRONG> the terminal description lists the most useful keys,
325 <STRONG>o</STRONG> the <EM>Enter</EM> key on the regular keyboard is already handled by the
326 standard ASCII characters for carriage-return and line-feed,
328 <STRONG>o</STRONG> depending on whether <STRONG>nl</STRONG> or <STRONG>nonl</STRONG> was called, pressing "Enter" on the
329 regular keyboard may return either a carriage-return or line-feed,
332 <STRONG>o</STRONG> "Enter or send" is the standard description for this key.
334 When using <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>, or <STRONG>mvwgetch</STRONG>, nocbreak mode
335 (<STRONG>nocbreak</STRONG>) and echo mode (<STRONG>echo</STRONG>) should not be used at the same time.
336 Depending on the state of the tty driver when each character is typed,
337 the program may produce undesirable results.
339 Note that <STRONG>getch</STRONG>, <STRONG>mvgetch</STRONG>, and <STRONG>mvwgetch</STRONG> may be macros.
341 Historically, the set of keypad macros was largely defined by the
342 extremely function-key-rich keyboard of the AT&T 7300, aka 3B1, aka
343 Safari 4. Modern personal computers usually have only a small subset
344 of these. IBM PC-style consoles typically support little more than
345 <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>,
346 <STRONG>KEY_PPAGE</STRONG>, and function keys 1 through 12. The Ins key is usually
347 mapped to <STRONG>KEY_IC</STRONG>.
350 </PRE><H2><a name="h2-EXTENSIONS">EXTENSIONS</a></H2><PRE>
351 <STRONG>has_key</STRONG> was designed for <STRONG><A HREF="ncurses.3x.html">ncurses(3x)</A></STRONG>, and is not found in SVr4 <EM>curses</EM>,
352 4.4BSD <EM>curses</EM>, or any other previous curses implementation.
355 </PRE><H2><a name="h2-PORTABILITY">PORTABILITY</a></H2><PRE>
356 Applications employing <EM>ncurses</EM> extensions should condition their use on
357 the visibility of the <STRONG>NCURSES_VERSION</STRONG> preprocessor macro.
359 X/Open Curses, Issue 4, Version 2, describes <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>,
360 <STRONG>mvwgetch</STRONG>, and <STRONG>ungetch</STRONG>. They read single-byte characters only. The
361 standard specifies that they return <STRONG>ERR</STRONG> on failure, but describes no
364 The echo behavior of these functions on input of <STRONG>KEY_</STRONG> or backspace
365 characters was not specified in the SVr4 documentation. This
366 description is adapted from X/Open Curses.
368 The behavior of <STRONG>getch</STRONG> and friends in the presence of signal handlers is
369 unspecified in the SVr4 documentation and X/Open Curses. Under
370 historical curses implementations, it varied depending on whether the
371 operating system's dispatch of a signal to a handler interrupts a
372 <STRONG>read(2)</STRONG> call in progress or not, and also (in some implementations)
373 whether an input timeout or non-blocking mode has been set.
375 <STRONG>KEY_MOUSE</STRONG> is mentioned in X/Open Curses, along with a few related <EM>term-</EM>
376 <EM>info</EM> capabilities, but no higher-level functions use the feature. The
377 implementation in <EM>ncurses</EM> is an extension.
379 <STRONG>KEY_RESIZE</STRONG> is an extension first implemented for <EM>ncurses.</EM> NetBSD
380 <EM>curses</EM> later added this extension.
382 Programmers concerned about portability should be prepared for either
383 of two cases: (a) signal receipt does not interrupt <STRONG>getch</STRONG>; (b) signal
384 receipt interrupts <STRONG>getch</STRONG> and causes it to return <STRONG>ERR</STRONG> with <STRONG>errno</STRONG> set to
385 <STRONG>EINTR</STRONG>.
387 The <STRONG>has_key</STRONG> function is unique to <EM>ncurses</EM>. We recommend that any code
388 using it be conditionalized on the <STRONG>NCURSES_VERSION</STRONG> feature macro.
391 </PRE><H2><a name="h2-SEE-ALSO">SEE ALSO</a></H2><PRE>
392 <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>,
393 <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>
395 Comparable functions in the wide-character (ncursesw) library are
396 described in <STRONG><A HREF="curs_get_wch.3x.html">curs_get_wch(3x)</A></STRONG>.
400 ncurses 6.4 2024-02-17 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>
404 <li><a href="#h2-NAME">NAME</a></li>
405 <li><a href="#h2-SYNOPSIS">SYNOPSIS</a></li>
406 <li><a href="#h2-DESCRIPTION">DESCRIPTION</a>
408 <li><a href="#h3-Reading-Characters">Reading Characters</a></li>
409 <li><a href="#h3-Keypad-Mode">Keypad Mode</a></li>
410 <li><a href="#h3-Ungetting-Characters">Ungetting Characters</a></li>
411 <li><a href="#h3-Predefined-Key-Codes">Predefined Key Codes</a></li>
412 <li><a href="#h3-Testing-Key-Codes">Testing Key Codes</a></li>
415 <li><a href="#h2-RETURN-VALUE">RETURN VALUE</a></li>
416 <li><a href="#h2-NOTES">NOTES</a></li>
417 <li><a href="#h2-EXTENSIONS">EXTENSIONS</a></li>
418 <li><a href="#h2-PORTABILITY">PORTABILITY</a></li>
419 <li><a href="#h2-SEE-ALSO">SEE ALSO</a></li>