xref: /netbsd-src/lib/libcurses/curses_input.3 (revision 43b427685529f8e82b4e5ab65e20f8cd631efa11)
1.\"	$NetBSD: curses_input.3,v 1.32 2024/05/14 10:40:41 uwe Exp $
2.\"
3.\" Copyright (c) 2002
4.\"	Brett Lymn (blymn@NetBSD.org, brett_lymn@yahoo.com.au)
5.\"
6.\" This code is donated to the NetBSD Foundation by the Author.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. The name of the Author may not be used to endorse or promote
17.\"    products derived from this software without specific prior written
18.\"    permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"
33.Dd May 14, 2024
34.Dt CURSES_INPUT 3
35.Os
36.Sh NAME
37.Nm curses_input ,
38.Nm getch ,
39.Nm wgetch ,
40.Nm mvgetch ,
41.Nm mvwgetch ,
42.Nm define_key ,
43.Nm keyok ,
44.Nm has_key ,
45.Nm getnstr ,
46.Nm wgetnstr ,
47.Nm mvgetnstr ,
48.Nm mvwgetnstr ,
49.Nm getstr ,
50.Nm wgetstr ,
51.Nm mvgetstr ,
52.Nm mvwgetstr ,
53.Nm keypad ,
54.Nm is_keypad ,
55.Nm notimeout ,
56.Nm timeout ,
57.Nm wtimeout ,
58.Nm nodelay ,
59.Nm ungetch ,
60.Nm set_escdelay
61.Nd curses input stream routines
62.Sh LIBRARY
63.Lb libcurses
64.Sh SYNOPSIS
65.In curses.h
66.Ft int
67.Fn getch "void"
68.Ft int
69.Fn wgetch "WINDOW *win"
70.Ft int
71.Fn mvgetch "int y" "int x"
72.Ft int
73.Fn mvwgetch "WINDOW *win" "int y" "int x"
74.Ft int
75.Fn keyok "int key_symbol" "bool flag"
76.Ft int
77.Fn has_key "int key_symbol"
78.Ft int
79.Fn define_key "const char *sequence" "int key_symbol"
80.Ft int
81.Fn getnstr "char *str" "int limit"
82.Ft int
83.Fn wgetnstr "WINDOW *win" "char *str" "int limit"
84.Ft int
85.Fn mvgetnstr "int y" "int x" "char *str" "int limit"
86.Ft int
87.Fn mvwgetnstr "WINDOW *win" "int y" "int x" "char *str" "int limit"
88.Ft int
89.Fn getstr "char *str"
90.Ft int
91.Fn wgetstr "WINDOW *win" "char *str"
92.Ft int
93.Fn mvgetstr "int y" "int x" "char *str"
94.Ft int
95.Fn mvwgetstr "WINDOW *win" "int y" "int x" "char *str"
96.Ft int
97.Fn keypad "WINDOW *win" "bool flag"
98.Ft bool
99.Fn is_keypad "const WINDOW *win"
100.Ft int
101.Fn notimeout "WINDOW *win" "bool flag"
102.Ft void
103.Fn timeout "int delay"
104.Ft void
105.Fn wtimeout "WINDOW *win" "int delay"
106.Ft int
107.Fn nodelay "WINDOW *win" "bool flag"
108.Ft int
109.Fn ungetch "int c"
110.Ft int
111.Fn set_escdelay "int escdelay"
112.Pp
113.Va extern int ESCDELAY ;
114.Sh DESCRIPTION
115These functions read characters and strings from the window input file
116descriptor.
117.Pp
118The
119.Fn getch
120function reads a character from the
121.Dv stdscr
122input file descriptor and returns it.
123If the
124.Fn keypad
125flag has been set to
126.Dv TRUE ,
127then
128.Fn getch
129will assemble multi-character key sequences into key symbols,
130If the terminal is resized,
131.Fn getch
132will return
133.Dv KEY_RESIZE ,
134regardless of the setting of
135.Fn keypad .
136Calling
137.Fn getch
138will cause an implicit
139.Fn refresh
140on
141.Dv stdscr .
142.Pp
143The
144.Fn wgetch
145function is the same as the
146.Fn getch
147function, excepting that it reads from the input file descriptor associated
148with the window specified by
149.Fa win .
150.Pp
151If the
152.Fn keypad
153flag is
154.Dv TRUE
155then the assembly of specific key symbols can be disabled by using the
156.Fn keyok
157function.
158If the
159.Fa flag
160is set to
161.Dv FALSE
162on a key symbol then
163.Fn getch
164will behave as if the character sequence associated with that key symbol
165was not recognised and will return the component characters one at a time to
166the caller.
167The
168.Fn is_keypad
169function returns
170.Dv TRUE
171if the
172.Fn
173keypad
174flag is set for the window specified by
175.Fa win .
176.Pp
177The
178.Fn has_key
179function takes a key value and returns
180.Dv TRUE
181if the current terminal recognises a key with that value, otherwise
182.Dv FALSE .
183.Pp
184Custom associations between sequences of characters and a key symbol can
185be made by using the
186.Fn define_key
187function.
188Normally, these associations are made by the information in the
189.Xr terminfo 5
190database but the
191.Fn define_key
192function gives the capability to remove or add more associations.
193If
194.Fn define_key
195is passed a non-NULL string in
196.Fa sequence
197it will associate that sequence with the key symbol passed in
198.Fa key_symbol .
199The key symbol may be one of the ones listed below or a custom value that
200is application defined.
201It is valid to have multiple character sequences map to the same key
202symbol and there are no constraints on the length of the sequence allowed.
203The assembly of custom sequences follow the same rules for inter-character
204timing and so forth as the
205.Xr terminfo 5
206derived ones.
207If
208.Fn define_key
209is passed a NULL in
210.Fa sequence
211then all associations for the key symbol in
212.Fa key_symbol
213will be deleted, this includes any associations that were derived from
214.Xr terminfo 5 .
215.Pp
216The
217.Fn mvgetch
218and
219.Fn mvwgetch
220functions are the same as the
221.Fn getch
222and
223.Fn wgetch
224functions, respectively, excepting that
225.Fn wmove
226is called to move the cursor to the position specified by
227.Fa y ,
228.Fa x
229before the character is read.
230.Pp
231Calling
232.Fn getnstr ,
233.Fn wgetnstr ,
234.Fn mvgetnstr
235or
236.Fn mvwgetnstr
237is effectively the same as calling
238.Fn getch
239repeatedly until a newline is received or the character limit
240.Fa limit
241is reached.
242Once this happens the string is
243.Dv NULL
244terminated and returned in
245.Fa str .
246During input, the normal curses input key processing is performed and
247affects the input buffer.
248The
249.Fn mvgetnstr
250function calls
251.Fn wmove
252to move the cursor to the position given by
253.Fa y ,
254.Fa x
255before getting the string,
256.Fn wgetnstr
257reads the input from the designated window,
258.Fn mvwgetnstr
259moves the cursor to the position given by
260.Fa y ,
261.Fa x
262before getting the input from the designated window.
263.Pp
264The functions
265.Fn getstr ,
266.Fn wgetstr ,
267.Fn mvgetstr ,
268and
269.Fn mvwgetstr
270are similar to
271.Fn getnstr ,
272.Fn wgetnstr ,
273.Fn mvgetnstr ,
274and
275.Fn mvwgetnstr ,
276respectively, excepting that there is no limit on the number of characters
277that may be inserted into
278.Fa str .
279This may cause the buffer to be overflowed, so their use is not recommended.
280.Pp
281The
282.Fn keypad
283function is used to affect how
284.Fn getch
285processes input characters.
286If
287.Fa flag
288is set to
289.Dv TRUE ,
290then
291.Fn getch
292will scan the input stream looking for multi-character key sequences
293that are emitted by some terminal function keys.
294If a recognised sequence of characters is found, then
295.Fn getch
296will collapse that sequence into an integer key symbol, as shown below.
297The default setting for the flag is
298.Dv FALSE .
299.Pp
300The
301.Fn notimeout
302function controls whether or not
303.Fn getch
304will wait indefinitely between characters in a multi-character key
305sequence or not.
306If
307.Fa flag
308is
309.Dv TRUE ,
310then there is no timeout applied between characters comprising a
311multi-character key sequence.
312If
313.Fa flag
314is
315.Dv FALSE ,
316then the component characters of a multi-character sequence must not
317have an inter-character gap of more than
318.Va ESCDELAY .
319If this timing is exceeded, then the multi-character key assembly is
320deemed to have failed and the characters read thus far are returned
321one at a time when
322.Fn getch
323is called.
324The default setting for the flag is
325.Dv FALSE .
326The default value of
327.Va ESCDELAY
328is 300ms.
329If
330.Va ESCDELAY
331is negative, no timeout is applied between characters comprising a
332multi-character key sequence.
333.Pp
334The
335.Fn timeout
336function affects the behaviour of
337.Fn getch
338when reading a character from
339.Dv stdscr .
340If
341.Fa delay
342is negative, then
343.Fn getch
344will block indefinitely on a read.
345If
346.Fa delay
347is 0, then
348.Fn getch
349will return immediately with
350.Dv ERR
351if there are no characters immediately available.
352If
353.Fa delay
354is a positive number, then
355.Fn getch
356will wait for that many milliseconds before returning and, if no character
357was available, then
358.Dv ERR
359will be returned.
360Note that for a positive number, the timeout is only accurate to the nearest
361tenth of a second.
362Also, the maximum value of
363.Fa delay
364is 25500 milliseconds.
365The
366.Fn wtimeout
367function does the same as
368.Fn timeout
369but applies to the specified window
370.Fa win .
371.Pp
372The
373.Fn nodelay
374function turns on and off blocking reads for
375.Fn getch .
376If
377.Fa flag
378is
379.Dv TRUE ,
380then
381.Fn getch
382will not block on reads, if
383.Fa flag
384is
385.Dv FALSE ,
386then reads will block.
387The default setting for the flag is
388.Dv FALSE .
389.Fn nodelay win TRUE
390is equivalent to
391.Fn wtimeout win 0
392and
393.Fn nodelay win FALSE
394is equivalent to
395.Fn wtimeout win \-1 .
396.Pp
397.Fn ungetch
398will convert
399.Fa c
400into an unsigned char and push that character back onto the input stream.
401Only one character of push-back is guaranteed to work, more may be possible
402depending on system resources.
403.Pp
404The
405.Fn set_escdelay
406function sets the
407.Va ESCDELAY
408value of the current screen to
409.Fa escdelay .
410.Sh RETURN VALUES
411The functions
412.Fn getch ,
413.Fn wgetch ,
414.Fn mvgetch ,
415and
416.Fn mvwgetch
417will return the value of the key pressed or
418.Dv ERR
419in the case of an error or a timeout.
420Additionally, if
421.Fn keypad TRUE
422has been called on a window, then it may return one of the following values:
423.Pp
424.Bl -column "Termcap entry" "getch Return Value" "Key Function" -offset indent
425.It Sy "Termcap entry" Ta Sy "getch Return Value" Ta Sy "Key Function"
426.It \&!1 Ta KEY_SSAVE Ta Shift Save
427.It \&!2 Ta KEY_SSUSPEND Ta Shift Suspend
428.It \&!3 Ta KEY_SUNDO Ta Shift Undo
429.It \&#1 Ta KEY_SHELP Ta Shift Help
430.It \&#2 Ta KEY_SHOME Ta Shift Home
431.It \&#3 Ta KEY_SIC Ta Shift Insert Character
432.It \&#4 Ta KEY_SLEFT Ta Shift Left Arrow
433.It \&%0 Ta KEY_REDO Ta Redo
434.It \&%1 Ta KEY_HELP Ta Help
435.It \&%2 Ta KEY_MARK Ta Mark
436.It \&%3 Ta KEY_MESSAGE Ta Message
437.It \&%4 Ta KEY_MOVE Ta Move
438.It \&%5 Ta KEY_NEXT Ta Next Object
439.It \&%6 Ta KEY_OPEN Ta Open
440.It \&%7 Ta KEY_OPTIONS Ta Options
441.It \&%8 Ta KEY_PREVIOUS Ta Previous Object
442.It \&%9 Ta KEY_PRINT Ta Print
443.It \&%a Ta KEY_SMESSAGE Ta Shift Message
444.It \&%b Ta KEY_SMOVE Ta Shift Move
445.It \&%c Ta KEY_SNEXT Ta Shift Next Object
446.It \&%d Ta KEY_SOPTIONS Ta Shift Options
447.It \&%e Ta KEY_SPREVIOUS Ta Shift Previous Object
448.It \&%f Ta KEY_SPRINT Ta Shift Print
449.It \&%g Ta KEY_SREDO Ta Shift Redo
450.It \&%h Ta KEY_SREPLACE Ta Shift Replace
451.It \&%i Ta KEY_SRIGHT Ta Shift Right Arrow
452.It \&%j Ta KEY_SRSUME Ta Shift Resume
453.It \&&0 Ta KEY_SCANCEL Ta Shift Cancel
454.It \&&1 Ta KEY_REFERENCE Ta Reference
455.It \&&2 Ta KEY_REFRESH Ta Refresh
456.It \&&3 Ta KEY_REPLACE Ta Replace
457.It \&&4 Ta KEY_RESTART Ta Restart
458.It \&&5 Ta KEY_RESUME Ta Resume
459.It \&&6 Ta KEY_SAVE Ta Save
460.It \&&7 Ta KEY_SUSPEND Ta Suspend
461.It \&&8 Ta KEY_UNDO Ta Undo
462.It \&&9 Ta KEY_SBEG Ta Shift Begin
463.It \&*0 Ta KEY_SFIND Ta Shift Find
464.It \&*1 Ta KEY_SCOMMAND Ta Shift Command
465.It \&*2 Ta KEY_SCOPY Ta Shift Copy
466.It \&*3 Ta KEY_SCREATE Ta Shift Create
467.It \&*4 Ta KEY_SDC Ta Shift Delete Character
468.It \&*5 Ta KEY_SDL Ta Shift Delete Line
469.It \&*6 Ta KEY_SELECT Ta Select
470.It \&*7 Ta KEY_SEND Ta Shift End
471.It \&*8 Ta KEY_SEOL Ta Shift Clear to EOL
472.It \&*9 Ta KEY_SEXIT Ta Shift Exit
473.It \&@0 Ta KEY_FIND Ta Find
474.It \&@1 Ta KEY_BEG Ta Begin
475.It \&@2 Ta KEY_CANCEL Ta Cancel
476.It \&@3 Ta KEY_CLOSE Ta Close
477.It \&@4 Ta KEY_COMMAND Ta Command
478.It \&@5 Ta KEY_COPY Ta Copy
479.It \&@6 Ta KEY_CREATE Ta Create
480.It \&@7 Ta KEY_END Ta End
481.It \&@8 Ta KEY_ENTER Ta Enter
482.It \&@9 Ta KEY_EXIT Ta Exit
483.It \&F1 Ta KEY_F(11) Ta Function Key 11
484.It \&F2 Ta KEY_F(12) Ta Function Key 12
485.It \&F3 Ta KEY_F(13) Ta Function Key 13
486.It \&F4 Ta KEY_F(14) Ta Function Key 14
487.It \&F5 Ta KEY_F(15) Ta Function Key 15
488.It \&F6 Ta KEY_F(16) Ta Function Key 16
489.It \&F7 Ta KEY_F(17) Ta Function Key 17
490.It \&F8 Ta KEY_F(18) Ta Function Key 18
491.It \&F9 Ta KEY_F(19) Ta Function Key 19
492.It \&FA Ta KEY_F(20) Ta Function Key 20
493.It \&FB Ta KEY_F(21) Ta Function Key 21
494.It \&FC Ta KEY_F(22) Ta Function Key 22
495.It \&FD Ta KEY_F(23) Ta Function Key 23
496.It \&FE Ta KEY_F(24) Ta Function Key 24
497.It \&FF Ta KEY_F(25) Ta Function Key 25
498.It \&FG Ta KEY_F(26) Ta Function Key 26
499.It \&FH Ta KEY_F(27) Ta Function Key 27
500.It \&FI Ta KEY_F(28) Ta Function Key 28
501.It \&FJ Ta KEY_F(29) Ta Function Key 29
502.It \&FK Ta KEY_F(30) Ta Function Key 30
503.It \&FL Ta KEY_F(31) Ta Function Key 31
504.It \&FM Ta KEY_F(32) Ta Function Key 32
505.It \&FN Ta KEY_F(33) Ta Function Key 33
506.It \&FO Ta KEY_F(34) Ta Function Key 34
507.It \&FP Ta KEY_F(35) Ta Function Key 35
508.It \&FQ Ta KEY_F(36) Ta Function Key 36
509.It \&FR Ta KEY_F(37) Ta Function Key 37
510.It \&FS Ta KEY_F(38) Ta Function Key 38
511.It \&FT Ta KEY_F(39) Ta Function Key 39
512.It \&FU Ta KEY_F(40) Ta Function Key 40
513.It \&FV Ta KEY_F(41) Ta Function Key 41
514.It \&FW Ta KEY_F(42) Ta Function Key 42
515.It \&FX Ta KEY_F(43) Ta Function Key 43
516.It \&FY Ta KEY_F(44) Ta Function Key 44
517.It \&FZ Ta KEY_F(45) Ta Function Key 45
518.It \&Fa Ta KEY_F(46) Ta Function Key 46
519.It \&Fb Ta KEY_F(47) Ta Function Key 47
520.It \&Fc Ta KEY_F(48) Ta Function Key 48
521.It \&Fd Ta KEY_F(49) Ta Function Key 49
522.It \&Fe Ta KEY_F(50) Ta Function Key 50
523.It \&Ff Ta KEY_F(51) Ta Function Key 51
524.It \&Fg Ta KEY_F(52) Ta Function Key 52
525.It \&Fh Ta KEY_F(53) Ta Function Key 53
526.It \&Fi Ta KEY_F(54) Ta Function Key 54
527.It \&Fj Ta KEY_F(55) Ta Function Key 55
528.It \&Fk Ta KEY_F(56) Ta Function Key 56
529.It \&Fl Ta KEY_F(57) Ta Function Key 57
530.It \&Fm Ta KEY_F(58) Ta Function Key 58
531.It \&Fn Ta KEY_F(59) Ta Function Key 59
532.It \&Fo Ta KEY_F(60) Ta Function Key 60
533.It \&Fp Ta KEY_F(61) Ta Function Key 61
534.It \&Fq Ta KEY_F(62) Ta Function Key 62
535.It \&Fr Ta KEY_F(63) Ta Function Key 63
536.It \&K1 Ta KEY_A1 Ta Upper left key in keypad
537.It \&K2 Ta KEY_B2 Ta Centre key in keypad
538.It \&K3 Ta KEY_A3 Ta Upper right key in keypad
539.It \&K4 Ta KEY_C1 Ta Lower left key in keypad
540.It \&K5 Ta KEY_C3 Ta Lower right key in keypad
541.It \&Km Ta KEY_MOUSE Ta Mouse Event
542.It \&k0 Ta KEY_F0 Ta Function Key 0
543.It \&k1 Ta KEY_F(1) Ta Function Key 1
544.It \&k2 Ta KEY_F(2) Ta Function Key 2
545.It \&k3 Ta KEY_F(3) Ta Function Key 3
546.It \&k4 Ta KEY_F(4) Ta Function Key 4
547.It \&k5 Ta KEY_F(5) Ta Function Key 5
548.It \&k6 Ta KEY_F(6) Ta Function Key 6
549.It \&k7 Ta KEY_F(7) Ta Function Key 7
550.It \&k8 Ta KEY_F(8) Ta Function Key 8
551.It \&k9 Ta KEY_F(9) Ta Function Key 9
552.It \&k; Ta KEY_F(10) Ta Function Key 10
553.It \&kA Ta KEY_IL Ta Insert Line
554.It \&ka Ta KEY_CATAB Ta Clear All Tabs
555.It \&kB Ta KEY_BTAB Ta Back Tab
556.It \&kb Ta KEY_BACKSPACE Ta Backspace
557.It \&kC Ta KEY_CLEAR Ta Clear
558.It \&kD Ta KEY_DC Ta Delete Character
559.It \&kd Ta KEY_DOWN Ta Down Arrow
560.It \&kE Ta KEY_EOL Ta Clear to End Of Line
561.It \&kF Ta KEY_SF Ta Scroll Forward one line
562.It \&kH Ta KEY_LL Ta Home Down
563.It \&kh Ta KEY_HOME Ta Home
564.It \&kI Ta KEY_IC Ta Insert Character
565.It \&kL Ta KEY_DL Ta Delete Line
566.It \&kl Ta KEY_LEFT Ta Left Arrow
567.It \&kM Ta KEY_EIC Ta Exit Insert Character Mode
568.It \&kN Ta KEY_NPAGE Ta Next Page
569.It \&kP Ta KEY_PPAGE Ta Previous Page
570.It \&kR Ta KEY_SR Ta Scroll One Line Back
571.It \&kr Ta KEY_RIGHT Ta Right Arrow
572.It \&kS Ta KEY_EOS Ta Clear to End Of Screen
573.It \&kT Ta KEY_STAB Ta Set Tab
574.It \&kt Ta KEY_CTAB Ta Clear Tab
575.It \&ku Ta KEY_UP Ta Up Arrow
576.El
577.Pp
578Note that not all terminals are capable of generating all the keycodes
579listed above nor are terminfo entries normally configured with all the
580above capabilities defined.
581.Pp
582Other functions that return an int will return one of the following
583values:
584.Pp
585.Bl -tag -width ERR -compact
586.It Er OK
587The function completed successfully.
588.It Er ERR
589An error occurred in the function.
590.El
591.Pp
592Functions returning pointers will return
593.Dv NULL
594if an error is detected.
595.Sh SEE ALSO
596.Xr curses_cursor 3 ,
597.Xr curses_keyname 3 ,
598.Xr curses_refresh 3 ,
599.Xr curses_tty 3 ,
600.Xr terminfo 5
601.Sh STANDARDS
602The
603.Nx
604Curses library complies with the X/Open Curses specification, part of the
605Single Unix Specification.
606.Sh NOTES
607The
608.Fn keyok
609and
610.Fn define_key
611functions are implementations of extensions made by the NCurses library
612to the Curses standard.
613Portable implementations should avoid the use of these functions.
614.Sh HISTORY
615The Curses package appeared in
616.Bx 4.0 .
617The
618.Fn is_keypad
619and
620.Fn set_tabsize
621functions are
622.Em ncurses
623extension to the Curses library and was added in
624.Nx 8.0 .
625