3 ****************************************************************************
4 * Copyright 2018-2019,2020 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.55 2020/02/02 23:34:34 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</TITLE>
39 <link rel="author" href="mailto:bug-ncurses@gnu.org">
40 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
43 <H1 class="no-header">curs_getch 3x</H1>
45 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG> <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 <STRONG>curses</STRONG> 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>
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>
62 <STRONG>int</STRONG> <STRONG>ungetch(int</STRONG> <EM>ch</EM><STRONG>);</STRONG>
63 <STRONG>int</STRONG> <STRONG>has_key(int</STRONG> <EM>ch</EM><STRONG>);</STRONG>
66 </PRE><H2><a name="h2-DESCRIPTION">DESCRIPTION</a></H2><PRE>
68 </PRE><H3><a name="h3-Reading-characters">Reading characters</a></H3><PRE>
69 The <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG> and <STRONG>mvwgetch</STRONG>, routines read a character from
70 the window. In no-delay mode, if no input is waiting, the value <STRONG>ERR</STRONG> is
71 returned. In delay mode, the program waits until the system passes
72 text through to the program. Depending on the setting of <STRONG>cbreak</STRONG>, this
73 is after one character (cbreak mode), or after the first newline
74 (nocbreak mode). In half-delay mode, the program waits until a charac-
75 ter is typed or the specified timeout has been reached.
77 If <STRONG>echo</STRONG> is enabled, and the window is not a pad, then the character
78 will also be echoed into the designated window according to the follow-
81 <STRONG>o</STRONG> If the character is the current erase character, left arrow, or
82 backspace, the cursor is moved one space to the left and that
83 screen position is erased as if <STRONG>delch</STRONG> had been called.
85 <STRONG>o</STRONG> If the character value is any other <STRONG>KEY_</STRONG> define, the user is alert-
86 ed with a <STRONG>beep</STRONG> call.
88 <STRONG>o</STRONG> If the character is a carriage-return, and if <STRONG>nl</STRONG> is enabled, it is
89 translated to a line-feed after echoing.
91 <STRONG>o</STRONG> Otherwise the character is simply output to the screen.
93 If the window is not a pad, and it has been moved or modified since the
94 last call to <STRONG>wrefresh</STRONG>, <STRONG>wrefresh</STRONG> will be called before another character
98 </PRE><H3><a name="h3-Keypad-mode">Keypad mode</a></H3><PRE>
99 If <STRONG>keypad</STRONG> is <STRONG>TRUE</STRONG>, and a function key is pressed, the token for that
100 function key is returned instead of the raw characters:
102 <STRONG>o</STRONG> The predefined function keys are listed in <STRONG><curses.h></STRONG> as macros
103 with values outside the range of 8-bit characters. Their names be-
104 gin with <STRONG>KEY_</STRONG>.
106 <STRONG>o</STRONG> Other (user-defined) function keys which may be defined using <STRONG>de-</STRONG>
107 <STRONG><A HREF="define_key.3x.html">fine_key(3x)</A></STRONG> have no names, but also are expected to have values
108 outside the range of 8-bit characters.
110 Thus, a variable intended to hold the return value of a function key
111 must be of short size or larger.
113 When a character that could be the beginning of a function key is re-
114 ceived (which, on modern terminals, means an escape character), <STRONG>curses</STRONG>
115 sets a timer. If the remainder of the sequence does not come in within
116 the designated time, the character is passed through; otherwise, the
117 function key value is returned. For this reason, many terminals expe-
118 rience a delay between the time a user presses the escape key and the
119 escape is returned to the program.
121 In <STRONG>ncurses</STRONG>, the timer normally expires after the value in <STRONG>ESCDELAY</STRONG> (see
122 <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;
123 it is an infinite (or very large) value. Because function keys usually
124 begin with an escape character, the terminal may appear to hang in no-
125 timeout mode after pressing the escape key until another key is
129 </PRE><H3><a name="h3-Ungetting-characters">Ungetting characters</a></H3><PRE>
130 The <STRONG>ungetch</STRONG> routine places <EM>ch</EM> back onto the input queue to be returned
131 by the next call to <STRONG>wgetch</STRONG>. There is just one input queue for all win-
135 </PRE><H3><a name="h3-Predefined-key-codes">Predefined key-codes</a></H3><PRE>
136 The following special keys are defined in <STRONG><curses.h></STRONG>.
138 <STRONG>o</STRONG> Except for the special case <STRONG>KEY_RESIZE</STRONG>, it is necessary to enable
139 <STRONG>keypad</STRONG> for <STRONG>getch</STRONG> to return these codes.
141 <STRONG>o</STRONG> Not all of these are necessarily supported on any particular termi-
144 <STRONG>o</STRONG> The naming convention may seem obscure, with some apparent mis-
145 spellings (such as "RSUME" for "resume"). The names correspond to
146 the long terminfo capability names for the keys, and were defined
147 long ago, in the 1980s.
149 <EM>Name</EM> <EM>Key</EM> <EM>name</EM>
150 -------------------------------------------------
152 KEY_DOWN The four arrow keys ...
156 KEY_HOME Home key (upward+left arrow)
157 KEY_BACKSPACE Backspace
158 KEY_F0 Function keys; space for 64 keys
160 KEY_F(<EM>n</EM>) For 0 <= <EM>n</EM> <= 63
163 KEY_DC Delete character
164 KEY_IC Insert char or enter insert mode
165 KEY_EIC Exit insert char mode
166 KEY_CLEAR Clear screen
167 KEY_EOS Clear to end of screen
168 KEY_EOL Clear to end of line
169 KEY_SF Scroll 1 line forward
170 KEY_SR Scroll 1 line backward (reverse)
172 KEY_PPAGE Previous page
175 KEY_CATAB Clear all tabs
176 KEY_ENTER Enter or send
177 KEY_SRESET Soft (partial) reset
178 KEY_RESET Reset or hard reset
179 KEY_PRINT Print or copy
180 KEY_LL Home down or bottom (lower left)
181 KEY_A1 Upper left of keypad
182 KEY_A3 Upper right of keypad
184 KEY_B2 Center of keypad
185 KEY_C1 Lower left of keypad
186 KEY_C3 Lower right of keypad
187 KEY_BTAB Back tab key
188 KEY_BEG Beg(inning) key
189 KEY_CANCEL Cancel key
191 KEY_COMMAND Cmd (command) key
193 KEY_CREATE Create key
199 KEY_MESSAGE Message key
200 KEY_MOUSE Mouse event read
202 KEY_NEXT Next object key
204 KEY_OPTIONS Options key
205 KEY_PREVIOUS Previous object key
207 KEY_REFERENCE Ref(erence) key
208 KEY_REFRESH Refresh key
209 KEY_REPLACE Replace key
210 KEY_RESIZE Screen resized
211 KEY_RESTART Restart key
212 KEY_RESUME Resume key
214 KEY_SBEG Shifted beginning key
215 KEY_SCANCEL Shifted cancel key
216 KEY_SCOMMAND Shifted command key
217 KEY_SCOPY Shifted copy key
218 KEY_SCREATE Shifted create key
219 KEY_SDC Shifted delete char key
220 KEY_SDL Shifted delete line key
221 KEY_SELECT Select key
222 KEY_SEND Shifted end key
223 KEY_SEOL Shifted clear line key
224 KEY_SEXIT Shifted exit key
225 KEY_SFIND Shifted find key
226 KEY_SHELP Shifted help key
227 KEY_SHOME Shifted home key
228 KEY_SIC Shifted input key
229 KEY_SLEFT Shifted left arrow key
230 KEY_SMESSAGE Shifted message key
231 KEY_SMOVE Shifted move key
232 KEY_SNEXT Shifted next key
233 KEY_SOPTIONS Shifted options key
234 KEY_SPREVIOUS Shifted prev key
235 KEY_SPRINT Shifted print key
236 KEY_SREDO Shifted redo key
237 KEY_SREPLACE Shifted replace key
238 KEY_SRIGHT Shifted right arrow
239 KEY_SRSUME Shifted resume key
240 KEY_SSAVE Shifted save key
241 KEY_SSUSPEND Shifted suspend key
242 KEY_SUNDO Shifted undo key
243 KEY_SUSPEND Suspend key
246 Keypad is arranged like this:
248 +-----+------+-------+
249 | <STRONG>A1</STRONG> | <STRONG>up</STRONG> | <STRONG>A3</STRONG> |
250 +-----+------+-------+
251 |<STRONG>left</STRONG> | <STRONG>B2</STRONG> | <STRONG>right</STRONG> |
252 +-----+------+-------+
253 | <STRONG>C1</STRONG> | <STRONG>down</STRONG> | <STRONG>C3</STRONG> |
254 +-----+------+-------+
255 A few of these predefined values do <EM>not</EM> correspond to a real key:
257 <STRONG>o</STRONG> <STRONG>KEY_RESIZE</STRONG> is returned when the <STRONG>SIGWINCH</STRONG> signal has been detected
258 (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
259 whether or not <STRONG>keypad</STRONG> has been enabled.
261 <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
262 code relies upon whether or not <STRONG><A HREF="curs_inopts.3x.html">keypad(3x)</A></STRONG> has been enabled, be-
263 cause (e.g., with <EM>xterm</EM> mouse prototocol) ncurses must read escape
264 sequences, just like a function key.
267 </PRE><H3><a name="h3-Testing-key-codes">Testing key-codes</a></H3><PRE>
268 The <STRONG>has_key</STRONG> routine takes a key-code value from the above list, and re-
269 turns <STRONG>TRUE</STRONG> or <STRONG>FALSE</STRONG> according to whether the current terminal type rec-
270 ognizes a key with that value.
272 The library also supports these extensions:
274 <STRONG>define_key</STRONG>
275 defines a key-code for a given string (see <STRONG><A HREF="define_key.3x.html">define_key(3x)</A></STRONG>).
277 <STRONG>key_defined</STRONG>
278 checks if there is a key-code defined for a given string (see
279 <STRONG><A HREF="key_defined.3x.html">key_defined(3x)</A></STRONG>).
282 </PRE><H2><a name="h2-RETURN-VALUE">RETURN VALUE</a></H2><PRE>
283 All routines return the integer <STRONG>ERR</STRONG> upon failure and an integer value
284 other than <STRONG>ERR</STRONG> (<STRONG>OK</STRONG> in the case of <STRONG>ungetch</STRONG>) upon successful completion.
286 <STRONG>ungetch</STRONG>
287 returns <STRONG>ERR</STRONG> if there is no more room in the FIFO.
289 <STRONG>wgetch</STRONG>
290 returns <STRONG>ERR</STRONG> if the window pointer is null, or if its timeout
291 expires without having any data, or if the execution was inter-
292 rupted by a signal (<STRONG>errno</STRONG> will be set to <STRONG>EINTR</STRONG>).
294 Functions with a "mv" prefix first perform a cursor movement using
295 <STRONG>wmove</STRONG>, and return an error if the position is outside the window, or if
296 the window pointer is null.
299 </PRE><H2><a name="h2-NOTES">NOTES</a></H2><PRE>
300 Use of the escape key by a programmer for a single character function
301 is discouraged, as it will cause a delay of up to one second while the
302 keypad code looks for a following function-key sequence.
304 Some keys may be the same as commonly used control keys, e.g., <STRONG>KEY_EN-</STRONG>
305 <STRONG>TER</STRONG> versus control/M, <STRONG>KEY_BACKSPACE</STRONG> versus control/H. Some curses im-
306 plementations may differ according to whether they treat these control
307 keys specially (and ignore the terminfo), or use the terminfo defini-
308 tions. <STRONG>Ncurses</STRONG> uses the terminfo definition. If it says that <STRONG>KEY_EN-</STRONG>
309 <STRONG>TER</STRONG> is control/M, <STRONG>getch</STRONG> will return <STRONG>KEY_ENTER</STRONG> when you press control/M.
311 Generally, <STRONG>KEY_ENTER</STRONG> denotes the character(s) sent by the <EM>Enter</EM> key on
314 <STRONG>o</STRONG> the terminal description lists the most useful keys,
316 <STRONG>o</STRONG> the <EM>Enter</EM> key on the regular keyboard is already handled by the
317 standard ASCII characters for carriage-return and line-feed,
319 <STRONG>o</STRONG> depending on whether <STRONG>nl</STRONG> or <STRONG>nonl</STRONG> was called, pressing "Enter" on the
320 regular keyboard may return either a carriage-return or line-feed,
323 <STRONG>o</STRONG> "Enter or send" is the standard description for this key.
325 When using <STRONG>getch</STRONG>, <STRONG>wgetch</STRONG>, <STRONG>mvgetch</STRONG>, or <STRONG>mvwgetch</STRONG>, nocbreak mode
326 (<STRONG>nocbreak</STRONG>) and echo mode (<STRONG>echo</STRONG>) should not be used at the same time.
327 Depending on the state of the tty driver when each character is typed,
328 the program may produce undesirable results.
330 Note that <STRONG>getch</STRONG>, <STRONG>mvgetch</STRONG>, and <STRONG>mvwgetch</STRONG> may be macros.
332 Historically, the set of keypad macros was largely defined by the ex-
333 tremely function-key-rich keyboard of the AT&T 7300, aka 3B1, aka Sa-
334 fari 4. Modern personal computers usually have only a small subset of
335 these. IBM PC-style consoles typically support little more than
336 <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>,
337 <STRONG>KEY_PPAGE</STRONG>, and function keys 1 through 12. The Ins key is usually
338 mapped to <STRONG>KEY_IC</STRONG>.
341 </PRE><H2><a name="h2-PORTABILITY">PORTABILITY</a></H2><PRE>
342 The *get* functions are described in the XSI Curses standard, Issue 4.
343 They read single-byte characters only. The standard specifies that
344 they return <STRONG>ERR</STRONG> on failure, but specifies no error conditions.
346 The echo behavior of these functions on input of <STRONG>KEY_</STRONG> or backspace
347 characters was not specified in the SVr4 documentation. This descrip-
348 tion is adopted from the XSI Curses standard.
350 The behavior of <STRONG>getch</STRONG> and friends in the presence of handled signals is
351 unspecified in the SVr4 and XSI Curses documentation. Under historical
352 curses implementations, it varied depending on whether the operating
353 system's implementation of handled signal receipt interrupts a <STRONG>read(2)</STRONG>
354 call in progress or not, and also (in some implementations) depending
355 on whether an input timeout or non-blocking mode has been set.
357 <STRONG>KEY_MOUSE</STRONG> is mentioned in XSI Curses, along with a few related terminfo
358 capabilities, but no higher-level functions use the feature. The im-
359 plementation in ncurses is an extension.
361 <STRONG>KEY_RESIZE</STRONG> is an extension first implemented for ncurses. NetBSD curs-
362 es later added this extension.
364 Programmers concerned about portability should be prepared for either
365 of two cases: (a) signal receipt does not interrupt <STRONG>getch</STRONG>; (b) signal
366 receipt interrupts <STRONG>getch</STRONG> and causes it to return <STRONG>ERR</STRONG> with <STRONG>errno</STRONG> set to
367 <STRONG>EINTR</STRONG>.
369 The <STRONG>has_key</STRONG> function is unique to <STRONG>ncurses</STRONG>. We recommend that any code
370 using it be conditionalized on the <STRONG>NCURSES_VERSION</STRONG> feature macro.
373 </PRE><H2><a name="h2-SEE-ALSO">SEE ALSO</a></H2><PRE>
374 <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>,
375 <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>.
377 Comparable functions in the wide-character (ncursesw) library are de-
378 scribed in <STRONG><A HREF="curs_get_wch.3x.html">curs_get_wch(3x)</A></STRONG>.
382 <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>
386 <li><a href="#h2-NAME">NAME</a></li>
387 <li><a href="#h2-SYNOPSIS">SYNOPSIS</a></li>
388 <li><a href="#h2-DESCRIPTION">DESCRIPTION</a>
390 <li><a href="#h3-Reading-characters">Reading characters</a></li>
391 <li><a href="#h3-Keypad-mode">Keypad mode</a></li>
392 <li><a href="#h3-Ungetting-characters">Ungetting characters</a></li>
393 <li><a href="#h3-Predefined-key-codes">Predefined key-codes</a></li>
394 <li><a href="#h3-Testing-key-codes">Testing key-codes</a></li>
397 <li><a href="#h2-RETURN-VALUE">RETURN VALUE</a></li>
398 <li><a href="#h2-NOTES">NOTES</a></li>
399 <li><a href="#h2-PORTABILITY">PORTABILITY</a></li>
400 <li><a href="#h2-SEE-ALSO">SEE ALSO</a></li>