Search code examples
ncursesxterm

detecting KEY_BACKSPACE in ncurses


I am writing a ncurses program, and I need to detect KEY_BACKSPACE. I have test the program under 2 linux distros.

Under suse, it runs OK. But under archlinux, KEY_BACKSPACE is 263, but what I print from program got 127.

For suse, they're both 263.

I am using int ch = wgetch(somw_window) to get the keypress.


Solution

  • It depends: whether Backspace sends ASCII BS (8) or ASCII DEL (127) is configurable with many terminal emulators, such as xterm. The terminal description (selected by the TERM environment variable) has to be consistent with the terminal's configuration, otherwise curses will not see that the kbs capability (backspace-key) matches what your keyboard actually sends, and you'll just get the 8/127 value.

    Some terminals send only one or the other (but most of the hard-coded ones which you'll encounter, use ASCII DEL).

    Further reading (xterm FAQ):