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