Thursday, November 29, 2007

Special-function keys

In Turbo C, special keyboard character such as F1 or Ctrl-PgUp are indicated by a two-byte sequence with the getch function: If the first byte returned is 0, then the next byte indicates which special-function key has been pressed. The values for this second byte have been summarized in the fnkeys.h file, which also provides symbolic names for all of them.

Unfortunately, many function-key sequences available using getch in MS-DOS are not available at all using the ncurses library. (For example, F1 is available, but Ctrl-PgUp is not.) Others may or may not be available, depending on various factors. Consequently, while the TurboC library has mimicked this functionality as best it can -- or rather, as best I can -- you may still be in the position of having to change some of the special-function keys used by your program.

Here's a list of all special keys that I believe are handled by Turbo C's getch function, along with my observations of how they are handled by the TurboC library's getch function. Your
mileage may vary.

Special Key Symbol provided in fnkeys.h Under xterm Under KDE Konsole Under a text-mode login
Digits on numeric keypad n/a (0-9, of course) Ok Ok No! Treated as arrows, PgUp/PgDn.
Ctrl-A through Ctrl-Z C_A-C_Z Ok Ok Ok, except there is no Ctrl-Z.
Alt-A through Alt-Z A_A-A_Z Ok Ok, except that Alt-O has a slight delay before registering. Ok
F1 through F10 N_F1-N-F10 Ok Ok Ok
Shift-F1 though Shift-F10 S_F1-S_F10 Ok, except Shift-F5 terminates program Ok, except: Shift-F5 terminates program, and F9-F10 don't work. Ok, except: Shift-F5 terminates program, and F9-F10 don't work.
Alt-F1 through Alt-F10 A_F1-A_F10 No! (These are KDE hotkeys.) No! (These are KDE hotkeys.) No! (Switches between consoles.)
Ctrl-F1 through Ctrl-F10 C_F1-C_F10 No! (These are KDE hotkeys.) No! (These are KDE hotkeys.) No!
Alt-0 through Alt-9 A_0-A9 Ok Ok Ok
Arrows N_UP, N_DOWN, N_LEFT, N_RIGHT Ok Ok on standalone arrow keys, but not on numeric keypad. Ok
PgUp, PgDn N_PGUP, N_PGDN Ok Ok on standalone arrow keys, but not on numeric keypad. Ok
Home, End N_HOME, N_END No! Ok on standalone arrow keys, but not on numeric keypad. No!
Ins, Del N_INS, N_DEL Ok Ok on standalone arrow keys, but not on numeric keypad. Ok
Pad-minus, Pad-plus N_PADPLUS, N_PADMINUS Ok No, treated as regular +/-. No, treated as regular +/-.
Ctrl-right, Ctrl-left C_LEFT, C_RIGHT Ok on standalone arrow keys, but not on numeric keypad. No, treated as regular right/left. No, treated as regular right/left.
Ctrl-PgUp, Ctrl-PgDn C_PGUP, C_PGDN No! No, treated as regular PgUp/PgDn. No, treated as regular PgUp/PgDn.
Ctrl-Home, Ctrl-End C_HOME, C_END Ok on standalone arrow keys, but not on numeric keypad. No, treated as regular home/end. No!
Alt-minus, Alt-equals A_MINUS, A_EQUALS Ok Ok Ok on main keyboard, but not on the numeric keypad.
Esc N_ESC Ok, though there is a short delay before the keystroke registers. Ok, though there is a short delay before the keystroke registers. Ok, though there is a short delay before the keystroke registers.

I'd be happy to hear from anyone with a clear idea on how make this work more consistently, perhaps by using termcap/terminfo, of which I'm completely ignorant.

No comments: