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