• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`curses` --- Terminal handling for character-cell displays
2===============================================================
3
4.. module:: curses
5   :synopsis: An interface to the curses library, providing portable
6              terminal handling.
7   :platform: Unix
8
9.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
10.. sectionauthor:: Eric Raymond <esr@thyrsus.com>
11
12--------------
13
14The :mod:`curses` module provides an interface to the curses library, the
15de-facto standard for portable advanced terminal handling.
16
17While curses is most widely used in the Unix environment, versions are available
18for Windows, DOS, and possibly other systems as well.  This extension module is
19designed to match the API of ncurses, an open-source curses library hosted on
20Linux and the BSD variants of Unix.
21
22.. note::
23
24   Since version 5.4, the ncurses library decides how to interpret non-ASCII data
25   using the ``nl_langinfo`` function.  That means that you have to call
26   :func:`locale.setlocale` in the application and encode Unicode strings
27   using one of the system's available encodings.  This example uses the
28   system's default encoding::
29
30      import locale
31      locale.setlocale(locale.LC_ALL, '')
32      code = locale.getpreferredencoding()
33
34   Then use *code* as the encoding for :meth:`str.encode` calls.
35
36.. seealso::
37
38   Module :mod:`curses.ascii`
39      Utilities for working with ASCII characters, regardless of your locale settings.
40
41   Module :mod:`curses.panel`
42      A panel stack extension that adds depth to  curses windows.
43
44   Module :mod:`curses.textpad`
45      Editable text widget for curses supporting  :program:`Emacs`\ -like bindings.
46
47   :ref:`curses-howto`
48      Tutorial material on using curses with Python, by Andrew Kuchling and Eric
49      Raymond.
50
51   The :source:`Tools/demo/` directory in the Python source distribution contains
52   some example programs using the curses bindings provided by this module.
53
54
55.. _curses-functions:
56
57Functions
58---------
59
60The module :mod:`curses` defines the following exception:
61
62
63.. exception:: error
64
65   Exception raised when a curses library function returns an error.
66
67.. note::
68
69   Whenever *x* or *y* arguments to a function or a method are optional, they
70   default to the current cursor location. Whenever *attr* is optional, it defaults
71   to :const:`A_NORMAL`.
72
73The module :mod:`curses` defines the following functions:
74
75
76.. function:: baudrate()
77
78   Return the output speed of the terminal in bits per second.  On software
79   terminal emulators it will have a fixed high value. Included for historical
80   reasons; in former times, it was used to  write output loops for time delays and
81   occasionally to change interfaces depending on the line speed.
82
83
84.. function:: beep()
85
86   Emit a short attention sound.
87
88
89.. function:: can_change_color()
90
91   Return ``True`` or ``False``, depending on whether the programmer can change the colors
92   displayed by the terminal.
93
94
95.. function:: cbreak()
96
97   Enter cbreak mode.  In cbreak mode (sometimes called "rare" mode) normal tty
98   line buffering is turned off and characters are available to be read one by one.
99   However, unlike raw mode, special characters (interrupt, quit, suspend, and flow
100   control) retain their effects on the tty driver and calling program.  Calling
101   first :func:`raw` then :func:`cbreak` leaves the terminal in cbreak mode.
102
103
104.. function:: color_content(color_number)
105
106   Return the intensity of the red, green, and blue (RGB) components in the color
107   *color_number*, which must be between ``0`` and :const:`COLORS`.  A 3-tuple is
108   returned, containing the R,G,B values for the given color, which will be between
109   ``0`` (no component) and ``1000`` (maximum amount of component).
110
111
112.. function:: color_pair(color_number)
113
114   Return the attribute value for displaying text in the specified color.  This
115   attribute value can be combined with :const:`A_STANDOUT`, :const:`A_REVERSE`,
116   and the other :const:`A_\*` attributes.  :func:`pair_number` is the counterpart
117   to this function.
118
119
120.. function:: curs_set(visibility)
121
122   Set the cursor state.  *visibility* can be set to 0, 1, or 2, for invisible,
123   normal, or very visible.  If the terminal supports the visibility requested, the
124   previous cursor state is returned; otherwise, an exception is raised.  On many
125   terminals, the "visible" mode is an underline cursor and the "very visible" mode
126   is a block cursor.
127
128
129.. function:: def_prog_mode()
130
131   Save the current terminal mode as the "program" mode, the mode when the running
132   program is using curses.  (Its counterpart is the "shell" mode, for when the
133   program is not in curses.)  Subsequent calls to :func:`reset_prog_mode` will
134   restore this mode.
135
136
137.. function:: def_shell_mode()
138
139   Save the current terminal mode as the "shell" mode, the mode when the running
140   program is not using curses.  (Its counterpart is the "program" mode, when the
141   program is using curses capabilities.) Subsequent calls to
142   :func:`reset_shell_mode` will restore this mode.
143
144
145.. function:: delay_output(ms)
146
147   Insert an *ms* millisecond pause in output.
148
149
150.. function:: doupdate()
151
152   Update the physical screen.  The curses library keeps two data structures, one
153   representing the current physical screen contents and a virtual screen
154   representing the desired next state.  The :func:`doupdate` ground updates the
155   physical screen to match the virtual screen.
156
157   The virtual screen may be updated by a :meth:`noutrefresh` call after write
158   operations such as :meth:`addstr` have been performed on a window.  The normal
159   :meth:`refresh` call is simply :meth:`noutrefresh` followed by :func:`doupdate`;
160   if you have to update multiple windows, you can speed performance and perhaps
161   reduce screen flicker by issuing :meth:`noutrefresh` calls on all windows,
162   followed by a single :func:`doupdate`.
163
164
165.. function:: echo()
166
167   Enter echo mode.  In echo mode, each character input is echoed to the screen as
168   it is entered.
169
170
171.. function:: endwin()
172
173   De-initialize the library, and return terminal to normal status.
174
175
176.. function:: erasechar()
177
178   Return the user's current erase character.  Under Unix operating systems this
179   is a property of the controlling tty of the curses program, and is not set by
180   the curses library itself.
181
182
183.. function:: filter()
184
185   The :func:`.filter` routine, if used, must be called before :func:`initscr` is
186   called.  The effect is that, during those calls, :envvar:`LINES` is set to 1; the
187   capabilities clear, cup, cud, cud1, cuu1, cuu, vpa are disabled; and the home
188   string is set to the value of cr. The effect is that the cursor is confined to
189   the current line, and so are screen updates.  This may be used for enabling
190   character-at-a-time  line editing without touching the rest of the screen.
191
192
193.. function:: flash()
194
195   Flash the screen.  That is, change it to reverse-video and then change it back
196   in a short interval.  Some people prefer such as 'visible bell' to the audible
197   attention signal produced by :func:`beep`.
198
199
200.. function:: flushinp()
201
202   Flush all input buffers.  This throws away any  typeahead  that  has been typed
203   by the user and has not yet been processed by the program.
204
205
206.. function:: getmouse()
207
208   After :meth:`getch` returns :const:`KEY_MOUSE` to signal a mouse event, this
209   method should be call to retrieve the queued mouse event, represented as a
210   5-tuple ``(id, x, y, z, bstate)``. *id* is an ID value used to distinguish
211   multiple devices, and *x*, *y*, *z* are the event's coordinates.  (*z* is
212   currently unused.)  *bstate* is an integer value whose bits will be set to
213   indicate the type of event, and will be the bitwise OR of one or more of the
214   following constants, where *n* is the button number from 1 to 4:
215   :const:`BUTTONn_PRESSED`, :const:`BUTTONn_RELEASED`, :const:`BUTTONn_CLICKED`,
216   :const:`BUTTONn_DOUBLE_CLICKED`, :const:`BUTTONn_TRIPLE_CLICKED`,
217   :const:`BUTTON_SHIFT`, :const:`BUTTON_CTRL`, :const:`BUTTON_ALT`.
218
219
220.. function:: getsyx()
221
222   Return the current coordinates of the virtual screen cursor in y and x.  If
223   leaveok is currently true, then -1,-1 is returned.
224
225
226.. function:: getwin(file)
227
228   Read window related data stored in the file by an earlier :func:`putwin` call.
229   The routine then creates and initializes a new window using that data, returning
230   the new window object.
231
232
233.. function:: has_colors()
234
235   Return ``True`` if the terminal can display colors; otherwise, return ``False``.
236
237
238.. function:: has_ic()
239
240   Return ``True`` if the terminal has insert- and delete-character capabilities.
241   This function is included for historical reasons only, as all modern software
242   terminal emulators have such capabilities.
243
244
245.. function:: has_il()
246
247   Return ``True`` if the terminal has insert- and delete-line capabilities, or can
248   simulate  them  using scrolling regions. This function is included for
249   historical reasons only, as all modern software terminal emulators have such
250   capabilities.
251
252
253.. function:: has_key(ch)
254
255   Take a key value *ch*, and return ``True`` if the current terminal type recognizes
256   a key with that value.
257
258
259.. function:: halfdelay(tenths)
260
261   Used for half-delay mode, which is similar to cbreak mode in that characters
262   typed by the user are immediately available to the program. However, after
263   blocking for *tenths* tenths of seconds, an exception is raised if nothing has
264   been typed.  The value of *tenths* must be a number between ``1`` and ``255``.  Use
265   :func:`nocbreak` to leave half-delay mode.
266
267
268.. function:: init_color(color_number, r, g, b)
269
270   Change the definition of a color, taking the number of the color to be changed
271   followed by three RGB values (for the amounts of red, green, and blue
272   components).  The value of *color_number* must be between ``0`` and
273   :const:`COLORS`.  Each of *r*, *g*, *b*, must be a value between ``0`` and
274   ``1000``.  When :func:`init_color` is used, all occurrences of that color on the
275   screen immediately change to the new definition.  This function is a no-op on
276   most terminals; it is active only if :func:`can_change_color` returns ``1``.
277
278
279.. function:: init_pair(pair_number, fg, bg)
280
281   Change the definition of a color-pair.  It takes three arguments: the number of
282   the color-pair to be changed, the foreground color number, and the background
283   color number.  The value of *pair_number* must be between ``1`` and
284   ``COLOR_PAIRS - 1`` (the ``0`` color pair is wired to white on black and cannot
285   be changed).  The value of *fg* and *bg* arguments must be between ``0`` and
286   :const:`COLORS`.  If the color-pair was previously initialized, the screen is
287   refreshed and all occurrences of that color-pair are changed to the new
288   definition.
289
290
291.. function:: initscr()
292
293   Initialize the library. Return a :class:`WindowObject` which represents the
294   whole screen.
295
296   .. note::
297
298      If there is an error opening the terminal, the underlying curses library may
299      cause the interpreter to exit.
300
301
302.. function:: is_term_resized(nlines, ncols)
303
304   Return ``True`` if :func:`resize_term` would modify the window structure,
305   ``False`` otherwise.
306
307
308.. function:: isendwin()
309
310   Return ``True`` if :func:`endwin` has been called (that is, the  curses library has
311   been deinitialized).
312
313
314.. function:: keyname(k)
315
316   Return the name of the key numbered *k*.  The name of a key generating printable
317   ASCII character is the key's character.  The name of a control-key combination
318   is a two-character string consisting of a caret followed by the corresponding
319   printable ASCII character.  The name of an alt-key combination (128--255) is a
320   string consisting of the prefix 'M-' followed by the name of the corresponding
321   ASCII character.
322
323
324.. function:: killchar()
325
326   Return the user's current line kill character. Under Unix operating systems
327   this is a property of the controlling tty of the curses program, and is not set
328   by the curses library itself.
329
330
331.. function:: longname()
332
333   Return a string containing the terminfo long name field describing the current
334   terminal.  The maximum length of a verbose description is 128 characters.  It is
335   defined only after the call to :func:`initscr`.
336
337
338.. function:: meta(yes)
339
340   If *yes* is 1, allow 8-bit characters to be input. If *yes* is 0,  allow only
341   7-bit chars.
342
343
344.. function:: mouseinterval(interval)
345
346   Set the maximum time in milliseconds that can elapse between press and release
347   events in order for them to be recognized as a click, and return the previous
348   interval value.  The default value is 200 msec, or one fifth of a second.
349
350
351.. function:: mousemask(mousemask)
352
353   Set the mouse events to be reported, and return a tuple ``(availmask,
354   oldmask)``.   *availmask* indicates which of the specified mouse events can be
355   reported; on complete failure it returns 0.  *oldmask* is the previous value of
356   the given window's mouse event mask.  If this function is never called, no mouse
357   events are ever reported.
358
359
360.. function:: napms(ms)
361
362   Sleep for *ms* milliseconds.
363
364
365.. function:: newpad(nlines, ncols)
366
367   Create and return a pointer to a new pad data structure with the given number
368   of lines and columns.  A pad is returned as a window object.
369
370   A pad is like a window, except that it is not restricted by the screen size, and
371   is not necessarily associated with a particular part of the screen.  Pads can be
372   used when a large window is needed, and only a part of the window will be on the
373   screen at one time.  Automatic refreshes of pads (such as from scrolling or
374   echoing of input) do not occur.  The :meth:`refresh` and :meth:`noutrefresh`
375   methods of a pad require 6 arguments to specify the part of the pad to be
376   displayed and the location on the screen to be used for the display. The
377   arguments are *pminrow*, *pmincol*, *sminrow*, *smincol*, *smaxrow*, *smaxcol*; the *p*
378   arguments refer to the upper left corner of the pad region to be displayed and
379   the *s* arguments define a clipping box on the screen within which the pad region
380   is to be displayed.
381
382
383.. function:: newwin(nlines, ncols)
384              newwin(nlines, ncols, begin_y, begin_x)
385
386   Return a new window, whose left-upper corner is at  ``(begin_y, begin_x)``, and
387   whose height/width is  *nlines*/*ncols*.
388
389   By default, the window will extend from the  specified position to the lower
390   right corner of the screen.
391
392
393.. function:: nl()
394
395   Enter newline mode.  This mode translates the return key into newline on input,
396   and translates newline into return and line-feed on output. Newline mode is
397   initially on.
398
399
400.. function:: nocbreak()
401
402   Leave cbreak mode.  Return to normal "cooked" mode with line buffering.
403
404
405.. function:: noecho()
406
407   Leave echo mode.  Echoing of input characters is turned off.
408
409
410.. function:: nonl()
411
412   Leave newline mode.  Disable translation of return into newline on input, and
413   disable low-level translation of newline into newline/return on output (but this
414   does not change the behavior of ``addch('\n')``, which always does the
415   equivalent of return and line feed on the virtual screen).  With translation
416   off, curses can sometimes speed up vertical motion a little; also, it will be
417   able to detect the return key on input.
418
419
420.. function:: noqiflush()
421
422   When the :func:`noqiflush` routine is used, normal flush of input and output queues
423   associated with the INTR, QUIT and SUSP characters will not be done.  You may
424   want to call :func:`noqiflush` in a signal handler if you want output to
425   continue as though the interrupt had not occurred, after the handler exits.
426
427
428.. function:: noraw()
429
430   Leave raw mode. Return to normal "cooked" mode with line buffering.
431
432
433.. function:: pair_content(pair_number)
434
435   Return a tuple ``(fg, bg)`` containing the colors for the requested color pair.
436   The value of *pair_number* must be between ``1`` and ``COLOR_PAIRS - 1``.
437
438
439.. function:: pair_number(attr)
440
441   Return the number of the color-pair set by the attribute value *attr*.
442   :func:`color_pair` is the counterpart to this function.
443
444
445.. function:: putp(string)
446
447   Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
448   terminfo capability for the current terminal.  Note that the output of :func:`putp`
449   always goes to standard output.
450
451
452.. function:: qiflush( [flag] )
453
454   If *flag* is ``False``, the effect is the same as calling :func:`noqiflush`. If
455   *flag* is ``True``, or no argument is provided, the queues will be flushed when
456   these control characters are read.
457
458
459.. function:: raw()
460
461   Enter raw mode.  In raw mode, normal line buffering and  processing of
462   interrupt, quit, suspend, and flow control keys are turned off; characters are
463   presented to curses input functions one by one.
464
465
466.. function:: reset_prog_mode()
467
468   Restore the  terminal  to "program" mode, as previously saved  by
469   :func:`def_prog_mode`.
470
471
472.. function:: reset_shell_mode()
473
474   Restore the  terminal  to "shell" mode, as previously saved  by
475   :func:`def_shell_mode`.
476
477
478.. function:: resetty()
479
480   Restore the state of the terminal modes to what it was at the last call to
481   :func:`savetty`.
482
483
484.. function:: resize_term(nlines, ncols)
485
486   Backend function used by :func:`resizeterm`, performing most of the work;
487   when resizing the windows, :func:`resize_term` blank-fills the areas that are
488   extended.  The calling application should fill in these areas with
489   appropriate data.  The :func:`resize_term` function attempts to resize all
490   windows.  However, due to the calling convention of pads, it is not possible
491   to resize these without additional interaction with the application.
492
493
494.. function:: resizeterm(nlines, ncols)
495
496   Resize the standard and current windows to the specified dimensions, and
497   adjusts other bookkeeping data used by the curses library that record the
498   window dimensions (in particular the SIGWINCH handler).
499
500
501.. function:: savetty()
502
503   Save the current state of the terminal modes in a buffer, usable by
504   :func:`resetty`.
505
506
507.. function:: setsyx(y, x)
508
509   Set the virtual screen cursor to *y*, *x*. If *y* and *x* are both -1, then
510   leaveok is set.
511
512
513.. function:: setupterm([termstr, fd])
514
515   Initialize the terminal.  *termstr* is a string giving the terminal name; if
516   omitted, the value of the :envvar:`TERM` environment variable will be used.  *fd* is the
517   file descriptor to which any initialization sequences will be sent; if not
518   supplied, the file descriptor for ``sys.stdout`` will be used.
519
520
521.. function:: start_color()
522
523   Must be called if the programmer wants to use colors, and before any other color
524   manipulation routine is called.  It is good practice to call this routine right
525   after :func:`initscr`.
526
527   :func:`start_color` initializes eight basic colors (black, red,  green, yellow,
528   blue, magenta, cyan, and white), and two global variables in the :mod:`curses`
529   module, :const:`COLORS` and :const:`COLOR_PAIRS`, containing the maximum number
530   of colors and color-pairs the terminal can support.  It also restores the colors
531   on the terminal to the values they had when the terminal was just turned on.
532
533
534.. function:: termattrs()
535
536   Return a logical OR of all video attributes supported by the terminal.  This
537   information is useful when a curses program needs complete control over the
538   appearance of the screen.
539
540
541.. function:: termname()
542
543   Return the value of the environment variable :envvar:`TERM`, truncated to 14 characters.
544
545
546.. function:: tigetflag(capname)
547
548   Return the value of the Boolean capability corresponding to the terminfo
549   capability name *capname*.  The value ``-1`` is returned if *capname* is not a
550   Boolean capability, or ``0`` if it is canceled or absent from the terminal
551   description.
552
553
554.. function:: tigetnum(capname)
555
556   Return the value of the numeric capability corresponding to the terminfo
557   capability name *capname*.  The value ``-2`` is returned if *capname* is not a
558   numeric capability, or ``-1`` if it is canceled or absent from the terminal
559   description.
560
561
562.. function:: tigetstr(capname)
563
564   Return the value of the string capability corresponding to the terminfo
565   capability name *capname*.  ``None`` is returned if *capname* is not a string
566   capability, or is canceled or absent from the terminal description.
567
568
569.. function:: tparm(str[, ...])
570
571   Instantiate the string *str* with the supplied parameters, where *str* should
572   be a parameterized string obtained from the terminfo database.  E.g.
573   ``tparm(tigetstr("cup"), 5, 3)`` could result in ``b'\033[6;4H'``, the exact
574   result depending on terminal type.
575
576
577.. function:: typeahead(fd)
578
579   Specify that the file descriptor *fd* be used for typeahead checking.  If *fd*
580   is ``-1``, then no typeahead checking is done.
581
582   The curses library does "line-breakout optimization" by looking for typeahead
583   periodically while updating the screen.  If input is found, and it is coming
584   from a tty, the current update is postponed until refresh or doupdate is called
585   again, allowing faster response to commands typed in advance. This function
586   allows specifying a different file descriptor for typeahead checking.
587
588
589.. function:: unctrl(ch)
590
591   Return a string which is a printable representation of the character *ch*.
592   Control characters are displayed as a caret followed by the character, for
593   example as ``^C``. Printing characters are left as they are.
594
595
596.. function:: ungetch(ch)
597
598   Push *ch* so the next :meth:`getch` will return it.
599
600   .. note::
601
602      Only one *ch* can be pushed before :meth:`getch` is called.
603
604
605.. function:: update_lines_cols()
606
607   Update :envvar:`LINES` and :envvar:`COLS`. Useful for detecting manual screen resize.
608
609   .. versionadded:: 3.5
610
611
612.. function:: unget_wch(ch)
613
614   Push *ch* so the next :meth:`get_wch` will return it.
615
616   .. note::
617
618      Only one *ch* can be pushed before :meth:`get_wch` is called.
619
620   .. versionadded:: 3.3
621
622
623.. function:: ungetmouse(id, x, y, z, bstate)
624
625   Push a :const:`KEY_MOUSE` event onto the input queue, associating the given
626   state data with it.
627
628
629.. function:: use_env(flag)
630
631   If used, this function should be called before :func:`initscr` or newterm are
632   called.  When *flag* is ``False``, the values of lines and columns specified in the
633   terminfo database will be used, even if environment variables :envvar:`LINES`
634   and :envvar:`COLUMNS` (used by default) are set, or if curses is running in a
635   window (in which case default behavior would be to use the window size if
636   :envvar:`LINES` and :envvar:`COLUMNS` are not set).
637
638
639.. function:: use_default_colors()
640
641   Allow use of default values for colors on terminals supporting this feature. Use
642   this to support transparency in your application.  The default color is assigned
643   to the color number -1. After calling this function,  ``init_pair(x,
644   curses.COLOR_RED, -1)`` initializes, for instance, color pair *x* to a red
645   foreground color on the default background.
646
647
648.. function:: wrapper(func, ...)
649
650   Initialize curses and call another callable object, *func*, which should be the
651   rest of your curses-using application.  If the application raises an exception,
652   this function will restore the terminal to a sane state before re-raising the
653   exception and generating a traceback.  The callable object *func* is then passed
654   the main window 'stdscr' as its first argument, followed by any other arguments
655   passed to :func:`wrapper`.  Before calling *func*, :func:`wrapper` turns on
656   cbreak mode, turns off echo, enables the terminal keypad, and initializes colors
657   if the terminal has color support.  On exit (whether normally or by exception)
658   it restores cooked mode, turns on echo, and disables the terminal keypad.
659
660
661.. _curses-window-objects:
662
663Window Objects
664--------------
665
666Window objects, as returned by :func:`initscr` and :func:`newwin` above, have
667the following methods and attributes:
668
669
670.. method:: window.addch(ch[, attr])
671            window.addch(y, x, ch[, attr])
672
673   .. note::
674
675      A *character* means a C character (an ASCII code), rather than a Python
676      character (a string of length 1). (This note is true whenever the
677      documentation mentions a character.) The built-in :func:`ord` is handy for
678      conveying strings to codes.
679
680   Paint character *ch* at ``(y, x)`` with attributes *attr*, overwriting any
681   character previously painter at that location.  By default, the character
682   position and attributes are the current settings for the window object.
683
684
685.. method:: window.addnstr(str, n[, attr])
686            window.addnstr(y, x, str, n[, attr])
687
688   Paint at most *n* characters of the  string *str* at ``(y, x)`` with attributes
689   *attr*, overwriting anything previously on the display.
690
691
692.. method:: window.addstr(str[, attr])
693            window.addstr(y, x, str[, attr])
694
695   Paint the string *str* at ``(y, x)`` with attributes *attr*, overwriting
696   anything previously on the display.
697
698
699.. method:: window.attroff(attr)
700
701   Remove attribute *attr* from the "background" set applied to all writes to the
702   current window.
703
704
705.. method:: window.attron(attr)
706
707   Add attribute *attr* from the "background" set applied to all writes to the
708   current window.
709
710
711.. method:: window.attrset(attr)
712
713   Set the "background" set of attributes to *attr*.  This set is initially 0 (no
714   attributes).
715
716
717.. method:: window.bkgd(ch[, attr])
718
719   Set the background property of the window to the character *ch*, with
720   attributes *attr*.  The change is then applied to every character position in
721   that window:
722
723   * The attribute of every character in the window  is changed to the new
724     background attribute.
725
726   * Wherever  the  former background character appears, it is changed to the new
727     background character.
728
729
730.. method:: window.bkgdset(ch[, attr])
731
732   Set the window's background.  A window's background consists of a character and
733   any combination of attributes.  The attribute part of the background is combined
734   (OR'ed) with all non-blank characters that are written into the window.  Both
735   the character and attribute parts of the background are combined with the blank
736   characters.  The background becomes a property of the character and moves with
737   the character through any scrolling and insert/delete line/character operations.
738
739
740.. method:: window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])
741
742   Draw a border around the edges of the window. Each parameter specifies  the
743   character to use for a specific part of the border; see the table below for more
744   details.  The characters can be specified as integers or as one-character
745   strings.
746
747   .. note::
748
749      A ``0`` value for any parameter will cause the default character to be used for
750      that parameter.  Keyword parameters can *not* be used.  The defaults are listed
751      in this table:
752
753   +-----------+---------------------+-----------------------+
754   | Parameter | Description         | Default value         |
755   +===========+=====================+=======================+
756   | *ls*      | Left side           | :const:`ACS_VLINE`    |
757   +-----------+---------------------+-----------------------+
758   | *rs*      | Right side          | :const:`ACS_VLINE`    |
759   +-----------+---------------------+-----------------------+
760   | *ts*      | Top                 | :const:`ACS_HLINE`    |
761   +-----------+---------------------+-----------------------+
762   | *bs*      | Bottom              | :const:`ACS_HLINE`    |
763   +-----------+---------------------+-----------------------+
764   | *tl*      | Upper-left corner   | :const:`ACS_ULCORNER` |
765   +-----------+---------------------+-----------------------+
766   | *tr*      | Upper-right corner  | :const:`ACS_URCORNER` |
767   +-----------+---------------------+-----------------------+
768   | *bl*      | Bottom-left corner  | :const:`ACS_LLCORNER` |
769   +-----------+---------------------+-----------------------+
770   | *br*      | Bottom-right corner | :const:`ACS_LRCORNER` |
771   +-----------+---------------------+-----------------------+
772
773
774.. method:: window.box([vertch, horch])
775
776   Similar to :meth:`border`, but both *ls* and *rs* are *vertch* and both *ts* and
777   *bs* are *horch*.  The default corner characters are always used by this function.
778
779
780.. method:: window.chgat(attr)
781            window.chgat(num, attr)
782            window.chgat(y, x, attr)
783            window.chgat(y, x, num, attr)
784
785   Set the attributes of *num* characters at the current cursor position, or at
786   position ``(y, x)`` if supplied. If no value of *num* is given or *num* = -1,
787   the attribute will  be set on all the characters to the end of the line.  This
788   function does not move the cursor. The changed line will be touched using the
789   :meth:`touchline` method so that the contents will be redisplayed by the next
790   window refresh.
791
792
793.. method:: window.clear()
794
795   Like :meth:`erase`, but also cause the whole window to be repainted upon next
796   call to :meth:`refresh`.
797
798
799.. method:: window.clearok(yes)
800
801   If *yes* is 1, the next call to :meth:`refresh` will clear the window
802   completely.
803
804
805.. method:: window.clrtobot()
806
807   Erase from cursor to the end of the window: all lines below the cursor are
808   deleted, and then the equivalent of :meth:`clrtoeol` is performed.
809
810
811.. method:: window.clrtoeol()
812
813   Erase from cursor to the end of the line.
814
815
816.. method:: window.cursyncup()
817
818   Update the current cursor position of all the ancestors of the window to
819   reflect the current cursor position of the window.
820
821
822.. method:: window.delch([y, x])
823
824   Delete any character at ``(y, x)``.
825
826
827.. method:: window.deleteln()
828
829   Delete the line under the cursor. All following lines are moved up by one line.
830
831
832.. method:: window.derwin(begin_y, begin_x)
833            window.derwin(nlines, ncols, begin_y, begin_x)
834
835   An abbreviation for "derive window", :meth:`derwin` is the same as calling
836   :meth:`subwin`, except that *begin_y* and *begin_x* are relative to the origin
837   of the window, rather than relative to the entire screen.  Return a window
838   object for the derived window.
839
840
841.. method:: window.echochar(ch[, attr])
842
843   Add character *ch* with attribute *attr*, and immediately  call :meth:`refresh`
844   on the window.
845
846
847.. method:: window.enclose(y, x)
848
849   Test whether the given pair of screen-relative character-cell coordinates are
850   enclosed by the given window, returning ``True`` or ``False``.  It is useful for
851   determining what subset of the screen windows enclose the location of a mouse
852   event.
853
854
855.. attribute:: window.encoding
856
857   Encoding used to encode method arguments (Unicode strings and characters).
858   The encoding attribute is inherited from the parent window when a subwindow
859   is created, for example with :meth:`window.subwin`. By default, the locale
860   encoding is used (see :func:`locale.getpreferredencoding`).
861
862   .. versionadded:: 3.3
863
864
865.. method:: window.erase()
866
867   Clear the window.
868
869
870.. method:: window.getbegyx()
871
872   Return a tuple ``(y, x)`` of co-ordinates of upper-left corner.
873
874
875.. method:: window.getbkgd()
876
877   Return the given window's current background character/attribute pair.
878
879
880.. method:: window.getch([y, x])
881
882   Get a character. Note that the integer returned does *not* have to be in ASCII
883   range: function keys, keypad keys and so on return numbers higher than 256. In
884   no-delay mode, -1 is returned if there is no input, else :func:`getch` waits
885   until a key is pressed.
886
887
888.. method:: window.get_wch([y, x])
889
890   Get a wide character. Return a character for most keys, or an integer for
891   function keys, keypad keys, and other special keys.
892
893   .. versionadded:: 3.3
894
895
896.. method:: window.getkey([y, x])
897
898   Get a character, returning a string instead of an integer, as :meth:`getch`
899   does. Function keys, keypad keys and other special keys return a multibyte
900   string containing the key name.  In no-delay mode, an exception is raised if
901   there is no input.
902
903
904.. method:: window.getmaxyx()
905
906   Return a tuple ``(y, x)`` of the height and width of the window.
907
908
909.. method:: window.getparyx()
910
911   Return the beginning coordinates of this window relative to its parent window
912   into two integer variables y and x.  Return ``-1, -1`` if this window has no
913   parent.
914
915
916.. method:: window.getstr([y, x])
917
918   Read a string from the user, with primitive line editing capacity.
919
920
921.. method:: window.getyx()
922
923   Return a tuple ``(y, x)`` of current cursor position  relative to the window's
924   upper-left corner.
925
926
927.. method:: window.hline(ch, n)
928            window.hline(y, x, ch, n)
929
930   Display a horizontal line starting at ``(y, x)`` with length *n* consisting of
931   the character *ch*.
932
933
934.. method:: window.idcok(flag)
935
936   If *flag* is ``False``, curses no longer considers using the hardware insert/delete
937   character feature of the terminal; if *flag* is ``True``, use of character insertion
938   and deletion is enabled.  When curses is first initialized, use of character
939   insert/delete is enabled by default.
940
941
942.. method:: window.idlok(yes)
943
944   If called with *yes* equal to 1, :mod:`curses` will try and use hardware line
945   editing facilities. Otherwise, line insertion/deletion are disabled.
946
947
948.. method:: window.immedok(flag)
949
950   If *flag* is ``True``, any change in the window image automatically causes the
951   window to be refreshed; you no longer have to call :meth:`refresh` yourself.
952   However, it may degrade performance considerably, due to repeated calls to
953   wrefresh.  This option is disabled by default.
954
955
956.. method:: window.inch([y, x])
957
958   Return the character at the given position in the window. The bottom 8 bits are
959   the character proper, and upper bits are the attributes.
960
961
962.. method:: window.insch(ch[, attr])
963            window.insch(y, x, ch[, attr])
964
965   Paint character *ch* at ``(y, x)`` with attributes *attr*, moving the line from
966   position *x* right by one character.
967
968
969.. method:: window.insdelln(nlines)
970
971   Insert *nlines* lines into the specified window above the current line.  The
972   *nlines* bottom lines are lost.  For negative *nlines*, delete *nlines* lines
973   starting with the one under the cursor, and move the remaining lines up.  The
974   bottom *nlines* lines are cleared.  The current cursor position remains the
975   same.
976
977
978.. method:: window.insertln()
979
980   Insert a blank line under the cursor. All following lines are moved down by one
981   line.
982
983
984.. method:: window.insnstr(str, n[, attr])
985            window.insnstr(y, x, str, n[, attr])
986
987   Insert a character string (as many characters as will fit on the line) before
988   the character under the cursor, up to *n* characters.   If *n* is zero or
989   negative, the entire string is inserted. All characters to the right of the
990   cursor are shifted right, with the rightmost characters on the line being lost.
991   The cursor position does not change (after moving to *y*, *x*, if specified).
992
993
994.. method:: window.insstr(str[, attr])
995            window.insstr(y, x, str[, attr])
996
997   Insert a character string (as many characters as will fit on the line) before
998   the character under the cursor.  All characters to the right of the cursor are
999   shifted right, with the rightmost characters on the line being lost.  The cursor
1000   position does not change (after moving to *y*, *x*, if specified).
1001
1002
1003.. method:: window.instr([n])
1004            window.instr(y, x[, n])
1005
1006   Return a string of characters, extracted from the window starting at the
1007   current cursor position, or at *y*, *x* if specified. Attributes are stripped
1008   from the characters.  If *n* is specified, :meth:`instr` returns a string
1009   at most *n* characters long (exclusive of the trailing NUL).
1010
1011
1012.. method:: window.is_linetouched(line)
1013
1014   Return ``True`` if the specified line was modified since the last call to
1015   :meth:`refresh`; otherwise return ``False``.  Raise a :exc:`curses.error`
1016   exception if *line* is not valid for the given window.
1017
1018
1019.. method:: window.is_wintouched()
1020
1021   Return ``True`` if the specified window was modified since the last call to
1022   :meth:`refresh`; otherwise return ``False``.
1023
1024
1025.. method:: window.keypad(yes)
1026
1027   If *yes* is 1, escape sequences generated by some keys (keypad,  function keys)
1028   will be interpreted by :mod:`curses`. If *yes* is 0, escape sequences will be
1029   left as is in the input stream.
1030
1031
1032.. method:: window.leaveok(yes)
1033
1034   If *yes* is 1, cursor is left where it is on update, instead of being at "cursor
1035   position."  This reduces cursor movement where possible. If possible the cursor
1036   will be made invisible.
1037
1038   If *yes* is 0, cursor will always be at "cursor position" after an update.
1039
1040
1041.. method:: window.move(new_y, new_x)
1042
1043   Move cursor to ``(new_y, new_x)``.
1044
1045
1046.. method:: window.mvderwin(y, x)
1047
1048   Move the window inside its parent window.  The screen-relative parameters of
1049   the window are not changed.  This routine is used to display different parts of
1050   the parent window at the same physical position on the screen.
1051
1052
1053.. method:: window.mvwin(new_y, new_x)
1054
1055   Move the window so its upper-left corner is at ``(new_y, new_x)``.
1056
1057
1058.. method:: window.nodelay(yes)
1059
1060   If *yes* is ``1``, :meth:`getch` will be non-blocking.
1061
1062
1063.. method:: window.notimeout(yes)
1064
1065   If *yes* is ``1``, escape sequences will not be timed out.
1066
1067   If *yes* is ``0``, after a few milliseconds, an escape sequence will not be
1068   interpreted, and will be left in the input stream as is.
1069
1070
1071.. method:: window.noutrefresh()
1072
1073   Mark for refresh but wait.  This function updates the data structure
1074   representing the desired state of the window, but does not force an update of
1075   the physical screen.  To accomplish that, call  :func:`doupdate`.
1076
1077
1078.. method:: window.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1079
1080   Overlay the window on top of *destwin*. The windows need not be the same size,
1081   only the overlapping region is copied. This copy is non-destructive, which means
1082   that the current background character does not overwrite the old contents of
1083   *destwin*.
1084
1085   To get fine-grained control over the copied region, the second form of
1086   :meth:`overlay` can be used. *sminrow* and *smincol* are the upper-left
1087   coordinates of the source window, and the other variables mark a rectangle in
1088   the destination window.
1089
1090
1091.. method:: window.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1092
1093   Overwrite the window on top of *destwin*. The windows need not be the same size,
1094   in which case only the overlapping region is copied. This copy is destructive,
1095   which means that the current background character overwrites the old contents of
1096   *destwin*.
1097
1098   To get fine-grained control over the copied region, the second form of
1099   :meth:`overwrite` can be used. *sminrow* and *smincol* are the upper-left
1100   coordinates of the source window, the other variables mark a rectangle in the
1101   destination window.
1102
1103
1104.. method:: window.putwin(file)
1105
1106   Write all data associated with the window into the provided file object.  This
1107   information can be later retrieved using the :func:`getwin` function.
1108
1109
1110.. method:: window.redrawln(beg, num)
1111
1112   Indicate that the *num* screen lines, starting at line *beg*, are corrupted and
1113   should be completely redrawn on the next :meth:`refresh` call.
1114
1115
1116.. method:: window.redrawwin()
1117
1118   Touch the entire window, causing it to be completely redrawn on the next
1119   :meth:`refresh` call.
1120
1121
1122.. method:: window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
1123
1124   Update the display immediately (sync actual screen with previous
1125   drawing/deleting methods).
1126
1127   The 6 optional arguments can only be specified when the window is a pad created
1128   with :func:`newpad`.  The additional parameters are needed to indicate what part
1129   of the pad and screen are involved. *pminrow* and *pmincol* specify the upper
1130   left-hand corner of the rectangle to be displayed in the pad.  *sminrow*,
1131   *smincol*, *smaxrow*, and *smaxcol* specify the edges of the rectangle to be
1132   displayed on the screen.  The lower right-hand corner of the rectangle to be
1133   displayed in the pad is calculated from the screen coordinates, since the
1134   rectangles must be the same size.  Both rectangles must be entirely contained
1135   within their respective structures.  Negative values of *pminrow*, *pmincol*,
1136   *sminrow*, or *smincol* are treated as if they were zero.
1137
1138
1139.. method:: window.resize(nlines, ncols)
1140
1141   Reallocate storage for a curses window to adjust its dimensions to the
1142   specified values.  If either dimension is larger than the current values, the
1143   window's data is filled with blanks that have the current background
1144   rendition (as set by :meth:`bkgdset`) merged into them.
1145
1146
1147.. method:: window.scroll([lines=1])
1148
1149   Scroll the screen or scrolling region upward by *lines* lines.
1150
1151
1152.. method:: window.scrollok(flag)
1153
1154   Control what happens when the cursor of a window is moved off the edge of the
1155   window or scrolling region, either as a result of a newline action on the bottom
1156   line, or typing the last character of the last line.  If *flag* is false, the
1157   cursor is left on the bottom line.  If *flag* is true, the window is scrolled up
1158   one line.  Note that in order to get the physical scrolling effect on the
1159   terminal, it is also necessary to call :meth:`idlok`.
1160
1161
1162.. method:: window.setscrreg(top, bottom)
1163
1164   Set the scrolling region from line *top* to line *bottom*. All scrolling actions
1165   will take place in this region.
1166
1167
1168.. method:: window.standend()
1169
1170   Turn off the standout attribute.  On some terminals this has the side effect of
1171   turning off all attributes.
1172
1173
1174.. method:: window.standout()
1175
1176   Turn on attribute *A_STANDOUT*.
1177
1178
1179.. method:: window.subpad(begin_y, begin_x)
1180            window.subpad(nlines, ncols, begin_y, begin_x)
1181
1182   Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
1183   whose width/height is *ncols*/*nlines*.
1184
1185
1186.. method:: window.subwin(begin_y, begin_x)
1187            window.subwin(nlines, ncols, begin_y, begin_x)
1188
1189   Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
1190   whose width/height is *ncols*/*nlines*.
1191
1192   By default, the sub-window will extend from the specified position to the lower
1193   right corner of the window.
1194
1195
1196.. method:: window.syncdown()
1197
1198   Touch each location in the window that has been touched in any of its ancestor
1199   windows.  This routine is called by :meth:`refresh`, so it should almost never
1200   be necessary to call it manually.
1201
1202
1203.. method:: window.syncok(flag)
1204
1205   If called with *flag* set to ``True``, then :meth:`syncup` is called automatically
1206   whenever there is a change in the window.
1207
1208
1209.. method:: window.syncup()
1210
1211   Touch all locations in ancestors of the window that have been changed in  the
1212   window.
1213
1214
1215.. method:: window.timeout(delay)
1216
1217   Set blocking or non-blocking read behavior for the window.  If *delay* is
1218   negative, blocking read is used (which will wait indefinitely for input).  If
1219   *delay* is zero, then non-blocking read is used, and -1 will be returned by
1220   :meth:`getch` if no input is waiting.  If *delay* is positive, then
1221   :meth:`getch` will block for *delay* milliseconds, and return -1 if there is
1222   still no input at the end of that time.
1223
1224
1225.. method:: window.touchline(start, count[, changed])
1226
1227   Pretend *count* lines have been changed, starting with line *start*.  If
1228   *changed* is supplied, it specifies whether the affected lines are marked as
1229   having been changed (*changed*\ =1) or unchanged (*changed*\ =0).
1230
1231
1232.. method:: window.touchwin()
1233
1234   Pretend the whole window has been changed, for purposes of drawing
1235   optimizations.
1236
1237
1238.. method:: window.untouchwin()
1239
1240   Mark all lines in  the  window  as unchanged since the last call to
1241   :meth:`refresh`.
1242
1243
1244.. method:: window.vline(ch, n)
1245            window.vline(y, x, ch, n)
1246
1247   Display a vertical line starting at ``(y, x)`` with length *n* consisting of the
1248   character *ch*.
1249
1250
1251Constants
1252---------
1253
1254The :mod:`curses` module defines the following data members:
1255
1256
1257.. data:: ERR
1258
1259   Some curses routines  that  return  an integer, such as  :func:`getch`, return
1260   :const:`ERR` upon failure.
1261
1262
1263.. data:: OK
1264
1265   Some curses routines  that  return  an integer, such as  :func:`napms`, return
1266   :const:`OK` upon success.
1267
1268
1269.. data:: version
1270
1271   A string representing the current version of the module.  Also available as
1272   :const:`__version__`.
1273
1274Several constants are available to specify character cell attributes:
1275
1276+------------------+-------------------------------+
1277| Attribute        | Meaning                       |
1278+==================+===============================+
1279| ``A_ALTCHARSET`` | Alternate character set mode. |
1280+------------------+-------------------------------+
1281| ``A_BLINK``      | Blink mode.                   |
1282+------------------+-------------------------------+
1283| ``A_BOLD``       | Bold mode.                    |
1284+------------------+-------------------------------+
1285| ``A_DIM``        | Dim mode.                     |
1286+------------------+-------------------------------+
1287| ``A_NORMAL``     | Normal attribute.             |
1288+------------------+-------------------------------+
1289| ``A_REVERSE``    | Reverse background and        |
1290|                  | foreground colors.            |
1291+------------------+-------------------------------+
1292| ``A_STANDOUT``   | Standout mode.                |
1293+------------------+-------------------------------+
1294| ``A_UNDERLINE``  | Underline mode.               |
1295+------------------+-------------------------------+
1296
1297Keys are referred to by integer constants with names starting with  ``KEY_``.
1298The exact keycaps available are system dependent.
1299
1300.. XXX this table is far too large! should it be alphabetized?
1301
1302+-------------------+--------------------------------------------+
1303| Key constant      | Key                                        |
1304+===================+============================================+
1305| ``KEY_MIN``       | Minimum key value                          |
1306+-------------------+--------------------------------------------+
1307| ``KEY_BREAK``     | Break key (unreliable)                     |
1308+-------------------+--------------------------------------------+
1309| ``KEY_DOWN``      | Down-arrow                                 |
1310+-------------------+--------------------------------------------+
1311| ``KEY_UP``        | Up-arrow                                   |
1312+-------------------+--------------------------------------------+
1313| ``KEY_LEFT``      | Left-arrow                                 |
1314+-------------------+--------------------------------------------+
1315| ``KEY_RIGHT``     | Right-arrow                                |
1316+-------------------+--------------------------------------------+
1317| ``KEY_HOME``      | Home key (upward+left arrow)               |
1318+-------------------+--------------------------------------------+
1319| ``KEY_BACKSPACE`` | Backspace (unreliable)                     |
1320+-------------------+--------------------------------------------+
1321| ``KEY_F0``        | Function keys.  Up to 64 function keys are |
1322|                   | supported.                                 |
1323+-------------------+--------------------------------------------+
1324| ``KEY_Fn``        | Value of function key *n*                  |
1325+-------------------+--------------------------------------------+
1326| ``KEY_DL``        | Delete line                                |
1327+-------------------+--------------------------------------------+
1328| ``KEY_IL``        | Insert line                                |
1329+-------------------+--------------------------------------------+
1330| ``KEY_DC``        | Delete character                           |
1331+-------------------+--------------------------------------------+
1332| ``KEY_IC``        | Insert char or enter insert mode           |
1333+-------------------+--------------------------------------------+
1334| ``KEY_EIC``       | Exit insert char mode                      |
1335+-------------------+--------------------------------------------+
1336| ``KEY_CLEAR``     | Clear screen                               |
1337+-------------------+--------------------------------------------+
1338| ``KEY_EOS``       | Clear to end of screen                     |
1339+-------------------+--------------------------------------------+
1340| ``KEY_EOL``       | Clear to end of line                       |
1341+-------------------+--------------------------------------------+
1342| ``KEY_SF``        | Scroll 1 line forward                      |
1343+-------------------+--------------------------------------------+
1344| ``KEY_SR``        | Scroll 1 line backward (reverse)           |
1345+-------------------+--------------------------------------------+
1346| ``KEY_NPAGE``     | Next page                                  |
1347+-------------------+--------------------------------------------+
1348| ``KEY_PPAGE``     | Previous page                              |
1349+-------------------+--------------------------------------------+
1350| ``KEY_STAB``      | Set tab                                    |
1351+-------------------+--------------------------------------------+
1352| ``KEY_CTAB``      | Clear tab                                  |
1353+-------------------+--------------------------------------------+
1354| ``KEY_CATAB``     | Clear all tabs                             |
1355+-------------------+--------------------------------------------+
1356| ``KEY_ENTER``     | Enter or send (unreliable)                 |
1357+-------------------+--------------------------------------------+
1358| ``KEY_SRESET``    | Soft (partial) reset (unreliable)          |
1359+-------------------+--------------------------------------------+
1360| ``KEY_RESET``     | Reset or hard reset (unreliable)           |
1361+-------------------+--------------------------------------------+
1362| ``KEY_PRINT``     | Print                                      |
1363+-------------------+--------------------------------------------+
1364| ``KEY_LL``        | Home down or bottom (lower left)           |
1365+-------------------+--------------------------------------------+
1366| ``KEY_A1``        | Upper left of keypad                       |
1367+-------------------+--------------------------------------------+
1368| ``KEY_A3``        | Upper right of keypad                      |
1369+-------------------+--------------------------------------------+
1370| ``KEY_B2``        | Center of keypad                           |
1371+-------------------+--------------------------------------------+
1372| ``KEY_C1``        | Lower left of keypad                       |
1373+-------------------+--------------------------------------------+
1374| ``KEY_C3``        | Lower right of keypad                      |
1375+-------------------+--------------------------------------------+
1376| ``KEY_BTAB``      | Back tab                                   |
1377+-------------------+--------------------------------------------+
1378| ``KEY_BEG``       | Beg (beginning)                            |
1379+-------------------+--------------------------------------------+
1380| ``KEY_CANCEL``    | Cancel                                     |
1381+-------------------+--------------------------------------------+
1382| ``KEY_CLOSE``     | Close                                      |
1383+-------------------+--------------------------------------------+
1384| ``KEY_COMMAND``   | Cmd (command)                              |
1385+-------------------+--------------------------------------------+
1386| ``KEY_COPY``      | Copy                                       |
1387+-------------------+--------------------------------------------+
1388| ``KEY_CREATE``    | Create                                     |
1389+-------------------+--------------------------------------------+
1390| ``KEY_END``       | End                                        |
1391+-------------------+--------------------------------------------+
1392| ``KEY_EXIT``      | Exit                                       |
1393+-------------------+--------------------------------------------+
1394| ``KEY_FIND``      | Find                                       |
1395+-------------------+--------------------------------------------+
1396| ``KEY_HELP``      | Help                                       |
1397+-------------------+--------------------------------------------+
1398| ``KEY_MARK``      | Mark                                       |
1399+-------------------+--------------------------------------------+
1400| ``KEY_MESSAGE``   | Message                                    |
1401+-------------------+--------------------------------------------+
1402| ``KEY_MOVE``      | Move                                       |
1403+-------------------+--------------------------------------------+
1404| ``KEY_NEXT``      | Next                                       |
1405+-------------------+--------------------------------------------+
1406| ``KEY_OPEN``      | Open                                       |
1407+-------------------+--------------------------------------------+
1408| ``KEY_OPTIONS``   | Options                                    |
1409+-------------------+--------------------------------------------+
1410| ``KEY_PREVIOUS``  | Prev (previous)                            |
1411+-------------------+--------------------------------------------+
1412| ``KEY_REDO``      | Redo                                       |
1413+-------------------+--------------------------------------------+
1414| ``KEY_REFERENCE`` | Ref (reference)                            |
1415+-------------------+--------------------------------------------+
1416| ``KEY_REFRESH``   | Refresh                                    |
1417+-------------------+--------------------------------------------+
1418| ``KEY_REPLACE``   | Replace                                    |
1419+-------------------+--------------------------------------------+
1420| ``KEY_RESTART``   | Restart                                    |
1421+-------------------+--------------------------------------------+
1422| ``KEY_RESUME``    | Resume                                     |
1423+-------------------+--------------------------------------------+
1424| ``KEY_SAVE``      | Save                                       |
1425+-------------------+--------------------------------------------+
1426| ``KEY_SBEG``      | Shifted Beg (beginning)                    |
1427+-------------------+--------------------------------------------+
1428| ``KEY_SCANCEL``   | Shifted Cancel                             |
1429+-------------------+--------------------------------------------+
1430| ``KEY_SCOMMAND``  | Shifted Command                            |
1431+-------------------+--------------------------------------------+
1432| ``KEY_SCOPY``     | Shifted Copy                               |
1433+-------------------+--------------------------------------------+
1434| ``KEY_SCREATE``   | Shifted Create                             |
1435+-------------------+--------------------------------------------+
1436| ``KEY_SDC``       | Shifted Delete char                        |
1437+-------------------+--------------------------------------------+
1438| ``KEY_SDL``       | Shifted Delete line                        |
1439+-------------------+--------------------------------------------+
1440| ``KEY_SELECT``    | Select                                     |
1441+-------------------+--------------------------------------------+
1442| ``KEY_SEND``      | Shifted End                                |
1443+-------------------+--------------------------------------------+
1444| ``KEY_SEOL``      | Shifted Clear line                         |
1445+-------------------+--------------------------------------------+
1446| ``KEY_SEXIT``     | Shifted Dxit                               |
1447+-------------------+--------------------------------------------+
1448| ``KEY_SFIND``     | Shifted Find                               |
1449+-------------------+--------------------------------------------+
1450| ``KEY_SHELP``     | Shifted Help                               |
1451+-------------------+--------------------------------------------+
1452| ``KEY_SHOME``     | Shifted Home                               |
1453+-------------------+--------------------------------------------+
1454| ``KEY_SIC``       | Shifted Input                              |
1455+-------------------+--------------------------------------------+
1456| ``KEY_SLEFT``     | Shifted Left arrow                         |
1457+-------------------+--------------------------------------------+
1458| ``KEY_SMESSAGE``  | Shifted Message                            |
1459+-------------------+--------------------------------------------+
1460| ``KEY_SMOVE``     | Shifted Move                               |
1461+-------------------+--------------------------------------------+
1462| ``KEY_SNEXT``     | Shifted Next                               |
1463+-------------------+--------------------------------------------+
1464| ``KEY_SOPTIONS``  | Shifted Options                            |
1465+-------------------+--------------------------------------------+
1466| ``KEY_SPREVIOUS`` | Shifted Prev                               |
1467+-------------------+--------------------------------------------+
1468| ``KEY_SPRINT``    | Shifted Print                              |
1469+-------------------+--------------------------------------------+
1470| ``KEY_SREDO``     | Shifted Redo                               |
1471+-------------------+--------------------------------------------+
1472| ``KEY_SREPLACE``  | Shifted Replace                            |
1473+-------------------+--------------------------------------------+
1474| ``KEY_SRIGHT``    | Shifted Right arrow                        |
1475+-------------------+--------------------------------------------+
1476| ``KEY_SRSUME``    | Shifted Resume                             |
1477+-------------------+--------------------------------------------+
1478| ``KEY_SSAVE``     | Shifted Save                               |
1479+-------------------+--------------------------------------------+
1480| ``KEY_SSUSPEND``  | Shifted Suspend                            |
1481+-------------------+--------------------------------------------+
1482| ``KEY_SUNDO``     | Shifted Undo                               |
1483+-------------------+--------------------------------------------+
1484| ``KEY_SUSPEND``   | Suspend                                    |
1485+-------------------+--------------------------------------------+
1486| ``KEY_UNDO``      | Undo                                       |
1487+-------------------+--------------------------------------------+
1488| ``KEY_MOUSE``     | Mouse event has occurred                   |
1489+-------------------+--------------------------------------------+
1490| ``KEY_RESIZE``    | Terminal resize event                      |
1491+-------------------+--------------------------------------------+
1492| ``KEY_MAX``       | Maximum key value                          |
1493+-------------------+--------------------------------------------+
1494
1495On VT100s and their software emulations, such as X terminal emulators, there are
1496normally at least four function keys (:const:`KEY_F1`, :const:`KEY_F2`,
1497:const:`KEY_F3`, :const:`KEY_F4`) available, and the arrow keys mapped to
1498:const:`KEY_UP`, :const:`KEY_DOWN`, :const:`KEY_LEFT` and :const:`KEY_RIGHT` in
1499the obvious way.  If your machine has a PC keyboard, it is safe to expect arrow
1500keys and twelve function keys (older PC keyboards may have only ten function
1501keys); also, the following keypad mappings are standard:
1502
1503+------------------+-----------+
1504| Keycap           | Constant  |
1505+==================+===========+
1506| :kbd:`Insert`    | KEY_IC    |
1507+------------------+-----------+
1508| :kbd:`Delete`    | KEY_DC    |
1509+------------------+-----------+
1510| :kbd:`Home`      | KEY_HOME  |
1511+------------------+-----------+
1512| :kbd:`End`       | KEY_END   |
1513+------------------+-----------+
1514| :kbd:`Page Up`   | KEY_PPAGE |
1515+------------------+-----------+
1516| :kbd:`Page Down` | KEY_NPAGE |
1517+------------------+-----------+
1518
1519The following table lists characters from the alternate character set. These are
1520inherited from the VT100 terminal, and will generally be  available on software
1521emulations such as X terminals.  When there is no graphic available, curses
1522falls back on a crude printable ASCII approximation.
1523
1524.. note::
1525
1526   These are available only after :func:`initscr` has  been called.
1527
1528+------------------+------------------------------------------+
1529| ACS code         | Meaning                                  |
1530+==================+==========================================+
1531| ``ACS_BBSS``     | alternate name for upper right corner    |
1532+------------------+------------------------------------------+
1533| ``ACS_BLOCK``    | solid square block                       |
1534+------------------+------------------------------------------+
1535| ``ACS_BOARD``    | board of squares                         |
1536+------------------+------------------------------------------+
1537| ``ACS_BSBS``     | alternate name for horizontal line       |
1538+------------------+------------------------------------------+
1539| ``ACS_BSSB``     | alternate name for upper left corner     |
1540+------------------+------------------------------------------+
1541| ``ACS_BSSS``     | alternate name for top tee               |
1542+------------------+------------------------------------------+
1543| ``ACS_BTEE``     | bottom tee                               |
1544+------------------+------------------------------------------+
1545| ``ACS_BULLET``   | bullet                                   |
1546+------------------+------------------------------------------+
1547| ``ACS_CKBOARD``  | checker board (stipple)                  |
1548+------------------+------------------------------------------+
1549| ``ACS_DARROW``   | arrow pointing down                      |
1550+------------------+------------------------------------------+
1551| ``ACS_DEGREE``   | degree symbol                            |
1552+------------------+------------------------------------------+
1553| ``ACS_DIAMOND``  | diamond                                  |
1554+------------------+------------------------------------------+
1555| ``ACS_GEQUAL``   | greater-than-or-equal-to                 |
1556+------------------+------------------------------------------+
1557| ``ACS_HLINE``    | horizontal line                          |
1558+------------------+------------------------------------------+
1559| ``ACS_LANTERN``  | lantern symbol                           |
1560+------------------+------------------------------------------+
1561| ``ACS_LARROW``   | left arrow                               |
1562+------------------+------------------------------------------+
1563| ``ACS_LEQUAL``   | less-than-or-equal-to                    |
1564+------------------+------------------------------------------+
1565| ``ACS_LLCORNER`` | lower left-hand corner                   |
1566+------------------+------------------------------------------+
1567| ``ACS_LRCORNER`` | lower right-hand corner                  |
1568+------------------+------------------------------------------+
1569| ``ACS_LTEE``     | left tee                                 |
1570+------------------+------------------------------------------+
1571| ``ACS_NEQUAL``   | not-equal sign                           |
1572+------------------+------------------------------------------+
1573| ``ACS_PI``       | letter pi                                |
1574+------------------+------------------------------------------+
1575| ``ACS_PLMINUS``  | plus-or-minus sign                       |
1576+------------------+------------------------------------------+
1577| ``ACS_PLUS``     | big plus sign                            |
1578+------------------+------------------------------------------+
1579| ``ACS_RARROW``   | right arrow                              |
1580+------------------+------------------------------------------+
1581| ``ACS_RTEE``     | right tee                                |
1582+------------------+------------------------------------------+
1583| ``ACS_S1``       | scan line 1                              |
1584+------------------+------------------------------------------+
1585| ``ACS_S3``       | scan line 3                              |
1586+------------------+------------------------------------------+
1587| ``ACS_S7``       | scan line 7                              |
1588+------------------+------------------------------------------+
1589| ``ACS_S9``       | scan line 9                              |
1590+------------------+------------------------------------------+
1591| ``ACS_SBBS``     | alternate name for lower right corner    |
1592+------------------+------------------------------------------+
1593| ``ACS_SBSB``     | alternate name for vertical line         |
1594+------------------+------------------------------------------+
1595| ``ACS_SBSS``     | alternate name for right tee             |
1596+------------------+------------------------------------------+
1597| ``ACS_SSBB``     | alternate name for lower left corner     |
1598+------------------+------------------------------------------+
1599| ``ACS_SSBS``     | alternate name for bottom tee            |
1600+------------------+------------------------------------------+
1601| ``ACS_SSSB``     | alternate name for left tee              |
1602+------------------+------------------------------------------+
1603| ``ACS_SSSS``     | alternate name for crossover or big plus |
1604+------------------+------------------------------------------+
1605| ``ACS_STERLING`` | pound sterling                           |
1606+------------------+------------------------------------------+
1607| ``ACS_TTEE``     | top tee                                  |
1608+------------------+------------------------------------------+
1609| ``ACS_UARROW``   | up arrow                                 |
1610+------------------+------------------------------------------+
1611| ``ACS_ULCORNER`` | upper left corner                        |
1612+------------------+------------------------------------------+
1613| ``ACS_URCORNER`` | upper right corner                       |
1614+------------------+------------------------------------------+
1615| ``ACS_VLINE``    | vertical line                            |
1616+------------------+------------------------------------------+
1617
1618The following table lists the predefined colors:
1619
1620+-------------------+----------------------------+
1621| Constant          | Color                      |
1622+===================+============================+
1623| ``COLOR_BLACK``   | Black                      |
1624+-------------------+----------------------------+
1625| ``COLOR_BLUE``    | Blue                       |
1626+-------------------+----------------------------+
1627| ``COLOR_CYAN``    | Cyan (light greenish blue) |
1628+-------------------+----------------------------+
1629| ``COLOR_GREEN``   | Green                      |
1630+-------------------+----------------------------+
1631| ``COLOR_MAGENTA`` | Magenta (purplish red)     |
1632+-------------------+----------------------------+
1633| ``COLOR_RED``     | Red                        |
1634+-------------------+----------------------------+
1635| ``COLOR_WHITE``   | White                      |
1636+-------------------+----------------------------+
1637| ``COLOR_YELLOW``  | Yellow                     |
1638+-------------------+----------------------------+
1639
1640
1641:mod:`curses.textpad` --- Text input widget for curses programs
1642===============================================================
1643
1644.. module:: curses.textpad
1645   :synopsis: Emacs-like input editing in a curses window.
1646.. moduleauthor:: Eric Raymond <esr@thyrsus.com>
1647.. sectionauthor:: Eric Raymond <esr@thyrsus.com>
1648
1649
1650The :mod:`curses.textpad` module provides a :class:`Textbox` class that handles
1651elementary text editing in a curses window, supporting a set of keybindings
1652resembling those of Emacs (thus, also of Netscape Navigator, BBedit 6.x,
1653FrameMaker, and many other programs).  The module also provides a
1654rectangle-drawing function useful for framing text boxes or for other purposes.
1655
1656The module :mod:`curses.textpad` defines the following function:
1657
1658
1659.. function:: rectangle(win, uly, ulx, lry, lrx)
1660
1661   Draw a rectangle.  The first argument must be a window object; the remaining
1662   arguments are coordinates relative to that window.  The second and third
1663   arguments are the y and x coordinates of the upper left hand corner of the
1664   rectangle to be drawn; the fourth and fifth arguments are the y and x
1665   coordinates of the lower right hand corner. The rectangle will be drawn using
1666   VT100/IBM PC forms characters on terminals that make this possible (including
1667   xterm and most other software terminal emulators).  Otherwise it will be drawn
1668   with ASCII  dashes, vertical bars, and plus signs.
1669
1670
1671.. _curses-textpad-objects:
1672
1673Textbox objects
1674---------------
1675
1676You can instantiate a :class:`Textbox` object as follows:
1677
1678
1679.. class:: Textbox(win)
1680
1681   Return a textbox widget object.  The *win* argument should be a curses
1682   :class:`WindowObject` in which the textbox is to be contained. The edit cursor
1683   of the textbox is initially located at the upper left hand corner of the
1684   containing window, with coordinates ``(0, 0)``. The instance's
1685   :attr:`stripspaces` flag is initially on.
1686
1687   :class:`Textbox` objects have the following methods:
1688
1689
1690   .. method:: edit([validator])
1691
1692      This is the entry point you will normally use.  It accepts editing
1693      keystrokes until one of the termination keystrokes is entered.  If
1694      *validator* is supplied, it must be a function.  It will be called for
1695      each keystroke entered with the keystroke as a parameter; command dispatch
1696      is done on the result. This method returns the window contents as a
1697      string; whether blanks in the window are included is affected by the
1698      :attr:`stripspaces` attribute.
1699
1700
1701   .. method:: do_command(ch)
1702
1703      Process a single command keystroke.  Here are the supported special
1704      keystrokes:
1705
1706      +------------------+-------------------------------------------+
1707      | Keystroke        | Action                                    |
1708      +==================+===========================================+
1709      | :kbd:`Control-A` | Go to left edge of window.                |
1710      +------------------+-------------------------------------------+
1711      | :kbd:`Control-B` | Cursor left, wrapping to previous line if |
1712      |                  | appropriate.                              |
1713      +------------------+-------------------------------------------+
1714      | :kbd:`Control-D` | Delete character under cursor.            |
1715      +------------------+-------------------------------------------+
1716      | :kbd:`Control-E` | Go to right edge (stripspaces off) or end |
1717      |                  | of line (stripspaces on).                 |
1718      +------------------+-------------------------------------------+
1719      | :kbd:`Control-F` | Cursor right, wrapping to next line when  |
1720      |                  | appropriate.                              |
1721      +------------------+-------------------------------------------+
1722      | :kbd:`Control-G` | Terminate, returning the window contents. |
1723      +------------------+-------------------------------------------+
1724      | :kbd:`Control-H` | Delete character backward.                |
1725      +------------------+-------------------------------------------+
1726      | :kbd:`Control-J` | Terminate if the window is 1 line,        |
1727      |                  | otherwise insert newline.                 |
1728      +------------------+-------------------------------------------+
1729      | :kbd:`Control-K` | If line is blank, delete it, otherwise    |
1730      |                  | clear to end of line.                     |
1731      +------------------+-------------------------------------------+
1732      | :kbd:`Control-L` | Refresh screen.                           |
1733      +------------------+-------------------------------------------+
1734      | :kbd:`Control-N` | Cursor down; move down one line.          |
1735      +------------------+-------------------------------------------+
1736      | :kbd:`Control-O` | Insert a blank line at cursor location.   |
1737      +------------------+-------------------------------------------+
1738      | :kbd:`Control-P` | Cursor up; move up one line.              |
1739      +------------------+-------------------------------------------+
1740
1741      Move operations do nothing if the cursor is at an edge where the movement
1742      is not possible.  The following synonyms are supported where possible:
1743
1744      +------------------------+------------------+
1745      | Constant               | Keystroke        |
1746      +========================+==================+
1747      | :const:`KEY_LEFT`      | :kbd:`Control-B` |
1748      +------------------------+------------------+
1749      | :const:`KEY_RIGHT`     | :kbd:`Control-F` |
1750      +------------------------+------------------+
1751      | :const:`KEY_UP`        | :kbd:`Control-P` |
1752      +------------------------+------------------+
1753      | :const:`KEY_DOWN`      | :kbd:`Control-N` |
1754      +------------------------+------------------+
1755      | :const:`KEY_BACKSPACE` | :kbd:`Control-h` |
1756      +------------------------+------------------+
1757
1758      All other keystrokes are treated as a command to insert the given
1759      character and move right (with line wrapping).
1760
1761
1762   .. method:: gather()
1763
1764      Return the window contents as a string; whether blanks in the
1765      window are included is affected by the :attr:`stripspaces` member.
1766
1767
1768   .. attribute:: stripspaces
1769
1770      This attribute is a flag which controls the interpretation of blanks in
1771      the window.  When it is on, trailing blanks on each line are ignored; any
1772      cursor motion that would land the cursor on a trailing blank goes to the
1773      end of that line instead, and trailing blanks are stripped when the window
1774      contents are gathered.
1775