5 User Input 7 (continues on next page) The C curses library offers only very simple input mechanisms. Python’s curses module adds a basic text-input widget. (Other libraries such as Urwid have more extensive collections of widgets.) There are two methods for getting input from a window: It’s possible to not wait for the user using the nodelay() window method. After nodelay(True) , getch() and getkey() for the window become non-blocking. To signal that no input is ready, getch() returns curses. $_{ERR}$(a value of -1) and$_{getkey()}$ raises an exception. There’s also a$_{halfdelay()}$ function, which can be used to (in effect) set a timer on each$_{getch()}$ ; if no input becomes available within a specified delay (measured in tenths of a second), curses raises an exception. The$_{getch()}$ method returns an integer; if it’s between 0 and 255, it represents the ASCII code of the key pressed. Values greater than 255 are special keys such as Page Up, Home, or the cursor keys. You can compare the value returned to constants such as curses.KEY_PPAGE , curses.KEY_HOME , or curses.KEY_LEFT . The main loop of your program may look something like this: The$_{curses.ascii}$module supplies ASCII class membership functions that take either integer or 1-character string arguments; these may be useful in writing more readable tests for such loops. It also supplies conversion functions that take either integer or 1-character-string arguments and return the same type. For example,$_{curses.}$ ascii.ctrl() returns the control character corresponding to its argument. There’s also a method to retrieve an entire string,$_{getstr()}$ . It isn’t used very often, because its functionality is quite limited; the only editing keys available are the backspace key and the Enter key, which terminates the string. It can optionally be limited to a fixed number of characters. The$_{curses.textpad}$ module supplies a text box that supports an Emacs-like set of keybindings. Various methods of the$_{Textbox}$class support editing with input validation and gathering the edit results either with or without trailing spaces. Here’s an example: import curses from curses.textpadimport Textbox, rectangle def main(stdscr): stdscr.addstr(0,0,"Enter IM message: (hit Ctrl-G to send) ") editwin =curses.newwin(5,30 ,2,1) rectangle(stdscr, 1,0,1+5+1,1+30 +1) stdscr.refresh() curses.echo() # Enable echoing of characters # Get a 15-character string, with the cursor on the top line s =stdscr.getstr( 0,0,15 ) while True : c =stdscr.getch() if c ==ord('p'): PrintDocument() elif c ==ord('q'): break # Exit the while loop elif c ==curses.KEY_HOME: x =y =0 • getch() refreshes the screen and then waits for the user to hit a key, displaying the key if$_{echo()}$ has been called earlier. You can optionally specify a coordinate to which the cursor should be moved before pausing. • getkey() does the same thing but converts the integer to a string. Individual characters are returned as 1-character strings, and special keys such as function keys return longer strings containing a key name such as KEY_UP or$_{^G}$ .