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