]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_getch.3x
ncurses 6.4 - patch 20240323
[ncurses.git] / man / curs_getch.3x
1 '\" t
2 .\"***************************************************************************
3 .\" Copyright 2018-2023,2024 Thomas E. Dickey                                *
4 .\" Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
5 .\"                                                                          *
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:                 *
13 .\"                                                                          *
14 .\" The above copyright notice and this permission notice shall be included  *
15 .\" in all copies or substantial portions of the Software.                   *
16 .\"                                                                          *
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.                               *
24 .\"                                                                          *
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       *
28 .\" authorization.                                                           *
29 .\"***************************************************************************
30 .\"
31 .\" $Id: curs_getch.3x,v 1.83 2024/03/23 20:38:57 tom Exp $
32 .TH curs_getch 3X 2024-03-23 "ncurses @NCURSES_MAJOR@.@NCURSES_MINOR@" "Library calls"
33 .ie \n(.g \{\
34 .ds `` \(lq
35 .ds '' \(rq
36 .ds ^  \(ha
37 .\}
38 .el \{\
39 .ie t .ds `` ``
40 .el   .ds `` ""
41 .ie t .ds '' ''
42 .el   .ds '' ""
43 .ds       ^  ^
44 .\}
45 .
46 .ie \n(.g .ds : \:
47 .el       .ds : \" empty
48 .
49 .de bP
50 .ie n  .IP \(bu 4
51 .el    .IP \(bu 2
52 ..
53 .SH NAME
54 \fB\%getch\fP,
55 \fB\%wgetch\fP,
56 \fB\%mvgetch\fP,
57 \fB\%mvwgetch\fP,
58 \fB\%ungetch\fP,
59 \fB\%has_key\fP \-
60 get (or push back) characters from \fIcurses\fR terminal keyboard
61 .SH SYNOPSIS
62 .nf
63 .B #include <curses.h>
64 .PP
65 .B int getch(void);
66 .B int wgetch(WINDOW *\fIwin\fP);
67 .B int mvgetch(int \fIy\fP, int \fIx\fP);
68 .B int mvwgetch(WINDOW *\fIwin\fP, int \fIy\fP, int \fIx\fP);
69 .PP
70 .B int ungetch(int \fIch\fP);
71 .PP
72 .\" XXX: Move has_key into its own page like define_key and key_defined?
73 \fI/* extension */\fP
74 .B int has_key(int \fIch\fP);
75 .fi
76 .SH DESCRIPTION
77 .SS "Reading Characters"
78 .B \%wgetch
79 gathers a key stroke from the terminal keyboard associated with a
80 .I curses
81 window
82 .IR win .
83 \fB\%ncurses\fP(3X) describes the variants of this function.
84 .PP
85 When input is pending,
86 .B \%wgetch
87 returns an integer identifying the key stroke;
88 for alphanumeric and punctuation keys,
89 this value corresponds to the character encoding used by the terminal.
90 Use of the control key as a modifier often results in a distinct code.
91 The behavior of other keys depends on whether
92 .I win
93 is in keypad mode;
94 see subsection \*(``Keypad Mode\*('' below.
95 .PP
96 If no input is pending,
97 then if the no-delay flag is set in the window
98 (see \fB\%nodelay\fP(3X)),
99 the function returns
100 .BR ERR ;
101 otherwise,
102 .I curses
103 waits until the terminal has input.
104 If \fB\%cbreak\fP(3X)
105 has been called,
106 this happens after one character is read.
107 If \fB\%nocbreak\fP(3X)
108 has been called,
109 it occurs when the next newline is read.
110 If \fB\%halfdelay\fP(3X)
111 has been called,
112 .I curses
113 waits until a character is typed or the specified delay elapses.
114 .PP
115 If \fB\%echo\fP(3X) has been called,
116 and the window is not a pad,
117 .I curses
118 writes the returned character
119 .I ch
120 to the window
121 (at the cursor position)
122 per the following rules.
123 .bP
124 If
125 .I ch
126 matches the terminal's erase character,
127 the cursor moves leftward one position
128 and the new position is erased
129 as if \fB\%wmove\fP(3X) and then \fB\%wdelch\fP(3X) were called.
130 When the window's keypad mode is enabled
131 (see below),
132 .B \%KEY_LEFT
133 and
134 .B \%KEY_BACKSPACE
135 are handled the same way.
136 .bP
137 .I curses
138 writes any other
139 .I ch
140 to the window,
141 as with \fB\%wechochar\fP(3X).
142 .bP
143 If the window has been moved or modified since the last call to
144 \fB\%wrefresh\fP(3X),
145 .I curses
146 calls
147 .BR \%wrefresh .
148 .PP
149 If
150 .I ch
151 is a carriage return and \fBnl\fP(3X) has been called,
152 .B \%wgetch
153 returns the character code for newline
154 (line feed)
155 instead.
156 .SS "Keypad Mode"
157 To
158 .IR curses ,
159 key strokes not from the alphabetic section of the keyboard
160 (those corresponding to the ECMA-6 character set\(emsee
161 \fIascii\fP(7)\(emoptionally modified by either the control or shift
162 keys)
163 are treated as
164 .I function
165 keys.
166 (In
167 .IR curses ,
168 the term \*(``function key\*('' includes but is not limited to keycaps
169 engraved with \*(``F1\*('',
170 \*(``PF1\*('',
171 and so on.)
172 If the window is in keypad mode,
173 these produce a numeric code corresponding to the
174 .B KEY_
175 symbols listed in subsection \*(``Predefined Key Codes\*('' below;
176 otherwise,
177 they transmit a sequence of codes typically starting with the escape
178 character,
179 and which must be collected with multiple
180 .B \%wgetch
181 calls.
182 .bP
183 The
184 .I \%curses.h
185 header file declares many
186 .I "predefined function keys"
187 whose names begin with
188 .BR KEY_ ;
189 these object-like macros have values outside the range of eight-bit
190 character codes.
191 .bP
192 In
193 .IR \%ncurses ,
194 .I "user-defined function keys"
195 are configured with \fBdefine_key\fP(3X);
196 they have no names,
197 but are also expected to have values outside the range of eight-bit
198 codes.
199 .PP
200 A variable intended to hold a function key code must thus be of type
201 .I short
202 or larger.
203 .PP
204 Most terminals one encounters follow the ECMA-48 standard insofar as
205 their function keys produce character sequences prefixed with the
206 escape character ESC.
207 This fact implies that
208 .I curses
209 cannot know whether the terminal has sent an ESC key stroke or the
210 beginning of a function key's character sequence without waiting to see
211 if,
212 and how soon,
213 further input arrives.
214 When
215 .I curses
216 reads such an ambiguous character,
217 it sets a timer.
218 If the remainder of the sequence does not arrive within the designated
219 time,
220 .B \%wgetch
221 returns the prefix character;
222 otherwise,
223 it returns the function key code corresponding to the unique sequence
224 defined by the terminal.
225 Consequently,
226 a user of a
227 .I curses
228 application may experience a delay after pressing ESC while
229 .I curses
230 disambiguates the input.
231 See section \*(``EXTENSIONS\*('' below.
232 If the window is in \*(``no time-out\*('' mode,
233 the timer does not expire;
234 it is an infinite
235 (or very large)
236 value.
237 See \fB\%notimeout\fP(3X).
238 Because function key sequences usually begin with an escape character,
239 the terminal may appear to hang in no time-out mode after the user has
240 pressed ESC.
241 Generally,
242 further typing \*(``awakens\*(''
243 .IR curses .
244 .SS "Ungetting Characters"
245 .B \%ungetch
246 places
247 .I ch
248 into the input queue to be returned by the next call to
249 .BR \%wgetch .
250 A single input queue serves all windows.
251 .SS "Predefined Key Codes"
252 The header file
253 .I \%curses.h
254 defines the following function key codes.
255 .bP
256 Except for the special case of
257 .BR \%KEY_RESIZE ,
258 a window's keypad mode must be enabled for
259 .B \%wgetch
260 to read these codes from it.
261 .bP
262 Not all of these are necessarily supported on any particular terminal.
263 .bP
264 The naming convention may seem obscure,
265 with some apparent misspellings
266 (such as \*(``RSUME\*('' for \*(``resume\*('');
267 The names correspond to the
268 .I \%term\%info
269 capability names for the keys,
270 and were standardized before the IBM PC/AT keyboard layout achieved a
271 dominant position in industry.
272 .PP
273 .RS
274 .\" XXX: Move this list into ncurses(3X), rather than duplicating it in
275 .\" get_wch(3X) or having that page cross reference this one?
276 .TS
277 Lb Lb
278 Lb Lx.
279 Symbol  Key name
280 =
281 KEY_BREAK       Break key
282 KEY_DOWN        Arrow keys
283 KEY_UP  \^
284 KEY_LEFT        \^
285 KEY_RIGHT       \^
286 KEY_HOME        Home key (upward+left arrow)
287 KEY_BACKSPACE   Backspace
288 KEY_F0  T{
289 Function keys; space for 64 keys is reserved
290 T}
291 KEY_F(\fIn\fP)  T{
292 Function key \fIn\fP where 0 \(<= \fIn\fP \(<= 63
293 T}
294 KEY_DL  Delete line
295 KEY_IL  Insert line
296 KEY_DC  Delete character
297 KEY_IC  Insert character/Enter insert mode
298 KEY_EIC Exit insert character mode
299 KEY_CLEAR       Clear screen
300 KEY_EOS Clear to end of screen
301 KEY_EOL Clear to end of line
302 KEY_SF  Scroll one line forward
303 KEY_SR  Scroll one line backward (reverse)
304 KEY_NPAGE       Next page/Page up
305 KEY_PPAGE       Previous page/Page down
306 KEY_STAB        Set tab
307 KEY_CTAB        Clear tab
308 KEY_CATAB       Clear all tabs
309 KEY_ENTER       Enter/Send
310 KEY_SRESET      Soft (partial) reset
311 KEY_RESET       (Hard) reset
312 KEY_PRINT       Print/Copy
313 KEY_LL  Home down/Bottom (lower left)
314 KEY_A1  Upper left of keypad
315 KEY_A3  Upper right of keypad
316 KEY_B2  Center of keypad
317 KEY_C1  Lower left of keypad
318 KEY_C3  Lower right of keypad
319 KEY_BTAB        Back tab key
320 KEY_BEG Beg(inning) key
321 KEY_CANCEL      Cancel key
322 KEY_CLOSE       Close key
323 KEY_COMMAND     Cmd (command) key
324 KEY_COPY        Copy key
325 KEY_CREATE      Create key
326 KEY_END End key
327 KEY_EXIT        Exit key
328 KEY_FIND        Find key
329 KEY_HELP        Help key
330 KEY_MARK        Mark key
331 KEY_MESSAGE     Message key
332 KEY_MOUSE       Mouse event occurred
333 KEY_MOVE        Move key
334 KEY_NEXT        Next object key
335 KEY_OPEN        Open key
336 KEY_OPTIONS     Options key
337 KEY_PREVIOUS    Previous object key
338 KEY_REDO        Redo key
339 KEY_REFERENCE   Ref(erence) key
340 KEY_REFRESH     Refresh key
341 KEY_REPLACE     Replace key
342 KEY_RESIZE      Screen resized
343 KEY_RESTART     Restart key
344 KEY_RESUME      Resume key
345 KEY_SAVE        Save key
346 KEY_SELECT      Select key
347 KEY_SUSPEND     Suspend key
348 KEY_UNDO        Undo key
349 _
350 KEY_SBEG        Shifted beginning key
351 KEY_SCANCEL     Shifted cancel key
352 KEY_SCOMMAND    Shifted command key
353 KEY_SCOPY       Shifted copy key
354 KEY_SCREATE     Shifted create key
355 KEY_SDC Shifted delete character key
356 KEY_SDL Shifted delete line key
357 KEY_SEND        Shifted end key
358 KEY_SEOL        Shifted clear line key
359 KEY_SEXIT       Shifted exit key
360 KEY_SFIND       Shifted find key
361 KEY_SHELP       Shifted help key
362 KEY_SHOME       Shifted home key
363 KEY_SIC Shifted insert key
364 KEY_SLEFT       Shifted left arrow key
365 KEY_SMESSAGE    Shifted message key
366 KEY_SMOVE       Shifted move key
367 KEY_SNEXT       Shifted next object key
368 KEY_SOPTIONS    Shifted options key
369 KEY_SPREVIOUS   Shifted previous object key
370 KEY_SPRINT      Shifted print key
371 KEY_SREDO       Shifted redo key
372 KEY_SREPLACE    Shifted replace key
373 KEY_SRIGHT      Shifted right arrow key
374 KEY_SRSUME      Shifted resume key
375 KEY_SSAVE       Shifted save key
376 KEY_SSUSPEND    Shifted suspend key
377 KEY_SUNDO       Shifted undo key
378 .TE
379 .RE
380 .PP
381 The keypad is arranged as follows.
382 .PP
383 .RS
384 .TS
385 allbox center;
386 C C C.
387 A1      up      A3
388 left    B2      right
389 C1      down    C3
390 .TE
391 .RE
392 .sp
393 Two of these symbols do
394 .I not
395 correspond to a real key.
396 .bP
397 .B \%wgetch
398 returns
399 .B \%KEY_RESIZE
400 (even if the window's keypad mode is disabled)
401 when
402 .I \%ncurses
403 detects the
404 .B \%SIGWINCH
405 signal;
406 see \fBinitscr\fP(3X) and \fBresizeterm\fP(3X).
407 .bP
408 .B \%wgetch
409 returns
410 .B \%KEY_MOUSE
411 to indicate that a mouse event is pending collection;
412 see \fBcurs_mouse\fP(3X).
413 Receipt of this code requires a window's keypad mode to be enabled,
414 because to interpret mouse input
415 (as with with \fIxterm\fP(1)'s mouse prototocol),
416 .I \%ncurses
417 must read an escape sequence,
418 as with a function key.
419 .SS "Testing Key Codes"
420 In
421 .IR \%ncurses ,
422 .B \%has_key
423 takes a key code value from the above list,
424 and returns a Boolean value indicating the terminal's recognition of it.
425 See also
426 \fBdefine_key\fP(3X) and \fBkey_defined\fP(3X).
427 .SH RETURN VALUE
428 .B \%wgetch
429 returns
430 .B ERR
431 if
432 .bP
433 the
434 .I \%WINDOW
435 pointer is
436 .BR NULL ,
437 or
438 .bP
439 its timeout expires without any data arriving,
440 or
441 .bP
442 execution was interrupted by a signal,
443 in which case
444 .B \%errno
445 is set to
446 .BR \%EINTR .
447 .PP
448 Functions with a \*(``mv\*('' prefix first perform cursor movement using
449 \fB\%wmove\fP(3X) and fail if the position is outside the window,
450 or
451 (for \*(``mvw\*('' functions)
452 if the
453 .I win
454 parameter is a null pointer.
455 .PP
456 .B \%ungetch
457 returns
458 .B OK
459 on success and
460 .B ERR
461 if there is no more room in the input queue.
462 .PP
463 .B \%has_key
464 returns
465 .B TRUE
466 or
467 .BR FALSE .
468 .SH NOTES
469 .I curses
470 discourages assignment of the ESC key to a discrete function by the
471 programmer because the library requires a delay while it awaits the
472 potential remainder of a terminal escape sequence.
473 .PP
474 Some key strokes are indistinguishable from control characters;
475 for example,
476 .B \%KEY_ENTER
477 may be the same as
478 .BR \*^M ,
479 .\" as with att630 or pccon+keys
480 and
481 .B \%KEY_BACKSPACE
482 may be the same as
483 .B \*^H
484 .\" as with att505 or vt52-basic
485 or
486 .BR \*^? .
487 .\" as with pccon+keys or vt320
488 Consult the terminal's
489 .I \%term\%info
490 entry to determine whether this is the case;
491 see \fB\%infocmp\fP(1).
492 Some
493 .I curses
494 implementations,
495 including
496 .IR \%ncurses ,
497 honor the
498 .I \%term\%info
499 key definitions;
500 others treat such control characters specially.
501 .PP
502 .I curses
503 distinguishes the Enter keys in the alphabetic and numeric keypad
504 sections of a keyboard because (most) terminals do.
505 .B \%KEY_ENTER
506 refers to the key on the (numeric) keypad and,
507 like other function keys,
508 is reliably recognized only if the window's keypad mode is enabled.
509 .bP
510 The
511 .I \%term\%info
512 .B \%key_enter
513 .RB ( kent )
514 capability describes the character (sequence) sent by the terminal's
515 keypad Enter key.
516 .bP
517 \*(``Enter or send\*('' is X/Open Curses's description of this key.
518 .PP
519 .I curses
520 treats the Enter or Return key in the
521 .I alphabetic
522 section of the keyboard differently.
523 .bP
524 It usually produces a control code for carriage return
525 .RB ( \*^M )
526 or line feed
527 .RB ( \*^J ).
528 .bP
529 Depending on the terminal mode
530 (raw,
531 cbreak,
532 or
533 \*(``cooked\*(''),
534 and whether \fB\%nl\fP(3X) or \fB\%nonl\fP(3X) has been called,
535 .B \%wgetch
536 may return either a carriage return or line feed upon an Enter or Return
537 key stroke.
538 .PP
539 Use of
540 .B \%wgetch
541 with \fB\%echo\fP(3X) and neither \fB\%cbreak\fP(3X) nor \fB\%raw\fP(3X)
542 is not well-defined.
543 .PP
544 Historically,
545 the list of key code macros above was influenced by the
546 function-key-rich keyboard of the AT&T 7300
547 (also known variously as the \*(``3B1\*('', \*(``Safari 4\*('', and
548 \*(``UNIX PC\*(''),
549 a 1985 machine.
550 Today's computer keyboards are based on the IBM PC/AT keyboard and tend
551 to have fewer.
552 A
553 .I curses
554 application can expect such a keyboard to transmit key codes
555 .BR \%KEY_UP ,
556 .BR \%KEY_DOWN ,
557 .BR \%KEY_LEFT ,
558 .BR \%KEY_RIGHT ,
559 .BR \%KEY_HOME ,
560 .BR \%KEY_END ,
561 .B \%KEY_PPAGE
562 (Page Up),
563 .B \%KEY_NPAGE
564 (Page Down),
565 .B \%KEY_IC
566 (Insert),
567 .B \%KEY_DC
568 (Delete),
569 and
570 .BI \%KEY_F( n )
571 for 1 \(<=
572 .I n
573 \(<= 12.
574 .PP
575 .BR \%getch ,
576 .BR \%mvgetch ,
577 and
578 .B \%mvwgetch
579 may be implemented as macros.
580 .SH EXTENSIONS
581 In
582 .IR \%ncurses ,
583 when a window's \*(``no time-out\*('' mode is
584 .I not
585 set,
586 the
587 .B \%ESCDELAY
588 variable configures the duration of the timer used to disambiguate a
589 function key character sequence from a series of key strokes beginning
590 with ESC typed by the user;
591 see
592 \fB\%curs_variables\fP(3X).
593 .PP
594 \fB\%has_key\fP was designed for \fB\%ncurses\fP(3X),
595 and is not found in SVr4
596 .IR curses ,
597 4.4BSD
598 .IR curses ,
599 or any other previous curses implementation.
600 .SH PORTABILITY
601 Applications employing
602 .I \%ncurses
603 extensions should condition their use on the visibility of the
604 .B \%NCURSES_VERSION
605 preprocessor macro.
606 .PP
607 X/Open Curses, Issue 4, describes
608 \fB\%getch\fP,
609 \fB\%wgetch\fP,
610 \fB\%mvgetch\fP,
611 \fB\%mvwgetch\fP,
612 and
613 \fB\%ungetch\fP.
614 It specifies no error conditions for them.
615 .PP
616 .B \%wgetch
617 reads only single-byte characters.
618 .PP
619 The echo behavior of these functions on input of
620 .B KEY_
621 or backspace characters was not specified in the SVr4 documentation.
622 This description is adapted from X/Open Curses.
623 .PP
624 The behavior of
625 .B \%wgetch
626 in the presence of signal handlers is unspecified in the SVr4
627 documentation and X/Open Curses.
628 In historical
629 .I curses
630 implementations,
631 it varied depending on whether the operating system's dispatch of a
632 signal to a handler interrupts a \fIread\fP(2) call in progress or not,
633 and also
634 (in some implementations)
635 whether an input timeout or non-blocking mode has been set.
636 Programmers concerned about portability should be prepared for either of
637 two cases:
638 (a) signal receipt does not interrupt
639 .BR \%wgetch ;
640 or
641 (b) signal receipt interrupts
642 .B \%wgetch
643 and causes it to return
644 .B ERR
645 with
646 .B \%errno
647 set to
648 .BR \%EINTR .
649 .PP
650 .B \%KEY_MOUSE
651 is mentioned in X/Open Curses,
652 along with a few related
653 .I \%term\%info
654 capabilities,
655 but no higher-level functions use the feature.
656 The implementation in
657 .I \%ncurses
658 is an extension.
659 .PP
660 .B \%KEY_RESIZE
661 and
662 .B \%has_key
663 are extensions first implemented for
664 .IR \%ncurses .
665 By 2022,
666 .I \%PDCurses
667 .\" https://web.archive.org/web/20220117232009/https://pdcurses.org/docs/MANUAL.html
668 and
669 NetBSD
670 .I curses
671 .\" https://web.archive.org/web/20200923185647/https://man.netbsd.org/curses_input.3
672 had added them along with
673 .BR \%KEY_MOUSE .
674 .SH SEE ALSO
675 \fB\%curses\fP(3X),
676 \fB\%curs_addch\fP(3X),
677 \fB\%curs_inopts\fP(3X),
678 \fB\%curs_mouse\fP(3X),
679 \fB\%curs_move\fP(3X),
680 \fB\%curs_outopts\fP(3X),
681 \fB\%curs_refresh\fP(3X),
682 \fB\%curs_variables\fP(3X),
683 \fB\%resizeterm\fP(3X),
684 \fB\%ascii\fP(7)
685 .PP
686 \fB\%curs_get_wch\fP(3X) describes comparable functions of the
687 .I \%ncurses
688 library in its wide-character configuration
689 .RI ( \%ncursesw ).
690 .PP
691 ECMA-6 \*(``7-bit coded Character Set\*(''
692 \%<https://\*:ecma\-international\*:.org/\
693 \*:publications\-and\-standards/\*:standards/\*:ecma\-6/>
694 .PP
695 ECMA-48 \*(``Control Functions for Coded Character Sets\*(''
696 \%<https://\*:ecma\-international\*:.org/\
697 \*:publications\-and\-standards/\*:standards/\*:ecma\-48/>