Next: , Previous: , Up: The basic curses library   [Contents][Index]


5.2.16 Getting characters from the keyboard

Procedure: getch win #:key y x

The getch routine reads a character from the terminal associated with the given window. In no-delay mode, if no input is waiting, the value #f is returned. In delay mode, the program waits until the system passes text through to the program. Depending on the setting of cbreak, this is after one character (cbreak mode), or after the first newline (nocbreak! mode). In halfdelay! mode, the program waits until a character is typed or the specified timeout has been reached.

Unless noecho! has been set, then the character will also be echoed into the designated window according to the following rules: If the character is the current erase character, left arrow, or backspace, the cursor is moved one space to the left and that screen position is erased as if delch had been called. If the character value is any other KEY_ define, the user is alerted with a beep call. Otherwise the character is simply output to the screen.

If keypad! is #t, and a function key is pressed, the integer keycode for that function key is returned instead of the raw characters. There is a list of possible function keys. See Table 5.3.

When a character that could be the beginning of a function key is received (which, on modern terminals, means an escape character), curses sets a timer. If the remainder of the sequence does not come in within the designated time, the character is passed through; otherwise, the function key value is returned. For this reason, many terminals experience a delay between the time a user presses the escape key and the escape is returned to the program.

If the window is not a pad, and it has been moved or modified since the last call to refresh, refresh will be called before another character is read.

Procedure: ungetch ch

The ungetch routine places ch back onto the input queue to be returned by the next call to getch. There is just one input queue for all windows.

It returns #f if there is no more space in the buffer, or #t otherwise.

Procedure: key-f n

This procedure returns the keycode for function key n.

The following function keys, might be returned by getch if keypad! has been enabled.

Note that almost all of these function keys do not exist on modern keyboards. The standard PC keyboard cannot be depended upon to have more than (key-f 1) through (key-f 12), KEY_PPAGE (Page Up), KEY_NPAGE (Page Down), KEY_HOME, KEY_END, KEY_IC (Insert), KEY_DC (Delete), KEY_BACKSPACE, KEY_DC (Delete), KEY_UP, KEY_DOWN, KEY_LEFT, and KEY_RIGHT.

Also, a common terminal is more likely to return C-M than KEY_ENTER when the RET key is pressed.

namedescription
KEY_BREAKBreak key
KEY_DOWNArrow down
KEY_UPArrow up
KEY_LEFTArrow left
KEY_RIGHTArrow right
KEY_HOMEHome key
KEY_BACKSPACEBackspace
KEY_F0Function key zero
KEY_DLDelete line
KEY_ILInsert line
KEY_DCDelete character
KEY_ICInsert char or enter insert mode
KEY_EICExit insert char mode
KEY_CLEARClear screen
KEY_EOSClear to end of screen
KEY_EOLClear to end of line
KEY_SFScroll 1 line forward
KEY_SRScroll 1 line backward (reverse)
KEY_NPAGENext page
KEY_PPAGEPrevious page
KEY_STABSet tab
KEY_CTABClear tab
KEY_CATABClear all tabs
KEY_ENTEREnter or send
KEY_SRESETSoft (partial) reset
KEY_RESETReset or hard reset
KEY_PRINTPrint or copy
KEY_LLHome down or bottom (lower left)
KEY_A1Upper left of keypad
KEY_A3Upper right of keypad
KEY_B2Center of keypad
KEY_C1Lower left of keypad
KEY_C3Lower right of keypad
KEY_BTABBack tab key
KEY_BEGBeg(inning) key
KEY_CANCELCancel key
KEY_CLOSEClose key
KEY_COMMANDCmd (command) key
KEY_COPYCopy key
KEY_CREATECreate key
KEY_ENDEnd key
KEY_EXITExit key
KEY_FINDFind key
KEY_HELPHelp key
KEY_MARKMark key
KEY_MESSAGEMessage key

Table 5.3: the keypad constants, part 1

namedescription
KEY_MOUSEMouse event read
KEY_MOVEMove key
KEY_NEXTNext object key
KEY_OPENOpen key
KEY_OPTIONSOptions key
KEY_PREVIOUSPrevious object key
KEY_REDORedo key
KEY_REFERENCERef(erence) key
KEY_REFRESHRefresh key
KEY_REPLACEReplace key
KEY_RESIZEScreen resized
KEY_RESTARTRestart key
KEY_RESUMEResume key
KEY_SAVESave key
KEY_SBEGShifted beginning key
KEY_SCANCELShifted cancel key
KEY_SCOMMANDShifted command key
KEY_SCOPYShifted copy key
KEY_SCREATEShifted create key
KEY_SDCShifted delete char key
KEY_SDLShifted delete line key
KEY_SELECTSelect key
KEY_SENDShifted end key
KEY_SEOLShifted clear line key
KEY_SEXITShifted exit key
KEY_SFINDShifted find key
KEY_SHELPShifted help key
KEY_SHOMEShifted home key
KEY_SICShifted input key
KEY_SLEFTShifted left arrow key
KEY_SMESSAGEShifted message key
KEY_SMOVEShifted move key
KEY_SNEXTShifted next key
KEY_SOPTIONSShifted options key
KEY_SPREVIOUSShifted prev key
KEY_SPRINTShifted print key
KEY_SREDOShifted redo key
KEY_SREPLACEShifted replace key
KEY_SRIGHTShifted right arrow
KEY_SRESUMEShifted resume key
KEY_SSAVEShifted save key
KEY_SSUSPENDShifted suspend key
KEY_SUNDOShifted undo key
KEY_SUSPENDSuspend key
KEY_UNDOUndo key

Table 5.4: the keypad constants, part 2

Procedure: has-key? ch

The has-key? routine takes a key value ch from the above list, and returns #t or #f according to whether the current terminal type recognizes a key with that value. Note that a few values do not correspond to a real key, e.g., KEY_RESIZE and KEY_MOUSE. See resizeterm for more details about KEY_RESIZE. For a discussion of KEY_MOUSE see Mouse handling.

Use of the ESC key by a programmer for a single character function is discouraged, as it will cause a delay of up to one second while the keypad code looks for a following function-key sequence.

Note that some keys may be the same as commonly used control keys, e.g., KEY_ENTER versus C-M, KEY_BACKSPACE versus C-H. Some curses implementations may differ according to whether they treat these control keys specially (and ignore the terminfo), or use the terminfo definitions. Ncurses uses the terminfo definition. If it says that KEY_ENTER is C-M, getch will return KEY_ENTER when you press C-M.

When using getch, wgetch, mvgetch, or mvwgetch, nocbreak mode (nocbreak!) and echo mode (echo!) should not be used at the same time. Depending on the state of the TTY driver when each character is typed, the program may produce undesirable results.

Historically, the set of keypad macros was largely defined by the extremely function-key-rich keyboard of the AT&T 7300, aka 3B1, aka Safari 4. Modern personal computers usually have only a small subset of these. IBM PC-style consoles typically support little more than KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_END, KEY_NPAGE, KEY_PPAGE, and function keys 1 through 12. The ins key is usually mapped to KEY_IC


Next: , Previous: , Up: The basic curses library   [Contents][Index]