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